Solved

import tab and comma delimited data in Matlab

Posted on 2011-03-17
5
961 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

912 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

16 Experts available now in Live!

Get 1:1 Help Now