Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

import tab and comma delimited data in Matlab

Posted on 2011-03-17
5
Medium Priority
?
1,045 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This video teaches viewers about errors in exception handling.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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