Solved

import tab and comma delimited data in Matlab

Posted on 2011-03-17
5
948 Views
Last Modified: 2016-03-02
I'm a newbie and am struggling to import a dat file which is a combination of tab and comma delimited data.

I have attached a sample dat file which is very easy to import in excel but in MATLAB i cannot get it to work.

I am trying to create a structure called mydat which will contain (see dat file, note the tabs and commas)

mydat.param1 = 1.01
mydat.param2 = 1.02
mydat.param3 = 1.03

mydat.curves  = [1.01 2.01 3.01 4.01;
                            1.02 2.02 3.02 4.02;
                            1.03 2.03 3.03 4.03]

mydat.curves_info = [data1; data2; data3; data4]

need help! mydat.txt
0
Comment
Question by:salamay
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:yuk99
Comment Utility
Here is the script to read this file:

% open the file and read all the data as strings skipping empty lines
filename = 'mydat.txt';
fid = fopen(filename,'r');
% textscan process the file with : or , as delimiters
C = textscan(fid,'%s','delimiter',':,','MultipleDelimsAsOne',1);
C = C{1};
fclose(fid);
% find param lines
pname = regexp(C,'^param\d','once','match');
pidx = find(~cellfun(@isempty,pname,'UniformOutput',true));
pname = pname(pidx);
% get param values
pval = str2double(C(pidx+1));
% create the structure
mydat = cell2struct(num2cell(pval),pname);
% find info lines and get the values
iidx = find(~cellfun(@isempty,regexp(C,'^info\d','once'),'UniformOutput',true));
rows = numel(iidx);
icell = reshape(C(iidx(end)+1:end),[],rows+1)';
% add the data to structure
mydat.curves = str2double(icell(2:end,:));
mydat.curves_info = strtrim(icell(1,:));
% clear temporary variables
clear C pname pidx pval iidx icell rows fid

Open in new window


See the comments and try to run it line by line reviewing variables to understand how it works.
Let me know if it's not doing exactly what you need.
0
 
LVL 1

Author Comment

by:salamay
Comment Utility
??? Error using ==> cell2struct
Duplicate field name "param2"

Error in ==> test2 at 15
mydat = cell2struct(num2cell(pval),pname);
0
 
LVL 1

Author Comment

by:salamay
Comment Utility
oh please ignore the above error.
My dat file had a duplicat field
0
 
LVL 1

Author Comment

by:salamay
Comment Utility
Well this is interesting but lets say i have

Time
Temp
Press

in place of

Param1
Param2
Param3

1. Could I use Regex for searching Time, Temp,  Press in a single statement?

2. Lets say if I dont know whats param1 2 or 3 (could be anything in that place) but i wanna get it in my structure knowing where they start and where they end (line 5 to Line 7), How can I do it?
0
 
LVL 15

Accepted Solution

by:
yuk99 earned 500 total points
Comment Utility
Replace lines 8-15 with the code below.
Note that it uses Parameteres line (wrong spelling?) in your file to find the parameters. It also assumes you always have -------- lines. If not, delete those lines using regexp in C variable before further processing.
The code also throw out the units of the parameters. Do you want to keep it somehow in the structure?
% find parameters lines
pidx1 = find(~cellfun(@isempty,regexp(C,'^Parameteres','once')));
pidx2 = find(~cellfun(@isempty,regexp(C,'^Information','once')));
% get parameters
pcell = reshape(C(pidx1+2:pidx2-2),2,[])';
pname = regexp(pcell(:,1),'^\w+','once','match');
% create the structure
mydat = cell2struct(num2cell(pcell(:,2)),pname);

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PYTHON: Updating local variable in .TPL file 24 122
Proactive Monitoring using PowerShell Script 3 63
bigDiff challenge 17 74
no14 challenge 14 56
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now