• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1055
  • Last Modified:

import tab and comma delimited data in Matlab

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
salamay
Asked:
salamay
  • 3
  • 2
1 Solution
 
yuk99Commented:
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
 
salamayAuthor Commented:
??? Error using ==> cell2struct
Duplicate field name "param2"

Error in ==> test2 at 15
mydat = cell2struct(num2cell(pval),pname);
0
 
salamayAuthor Commented:
oh please ignore the above error.
My dat file had a duplicat field
0
 
salamayAuthor Commented:
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
 
yuk99Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now