Solved

import tab and comma delimited data in Matlab

Posted on 2011-03-17
5
988 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
ID: 35168811
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
ID: 35169095
??? Error using ==> cell2struct
Duplicate field name "param2"

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

Author Comment

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

Author Comment

by:salamay
ID: 35169175
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
ID: 35172970
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Choosing the right language for new project 8 65
recursion example 16 141
WPF - Tooltips for ComboBox items 5 97
Adjust the position 3 65
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video teaches viewers about errors in exception handling.
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…

830 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