I'm working with converting a piece of Scilab code to MATLAB, where I want to process data in some text files that I got. The content of the text files may, for example, be on the form:
freq re(r1) im(r1) re(r2) im(r2) re(r3) im(r3) re(r4) im(r4)
1 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 9 1
2 3 4 5 6 7 7 2 3
etc (the first line is always a path to another file, and the second line always holds a number of variable names equal to the number of columns in the data matrix).
The thing is that the number of rows and columns with data in different files may vary, so I have to write my function to handle the general case. In the end, what I want to read into MATLAB for processing is the path to the s2p-file given at the first line of the text file, and the real data matrix which is separated by white-spaces.
In the original Scilab code this is done by the function fscanfMat (http://help.scilab.org/docs/5.3.2/en_US/fscanfMat.html), but I can't find any equivalent in MATLAB.
I've written a small hack in MATLAB which does the job for now, but I don't think it's a good piece of code, so I'm looking for hints for improving it, and maybe even make it a bit more "elegant".
The code I use atm is
function [A sfilename] = fscanfMat(filename)
dummyfile = ['dummy_' filename];
fid1 = fopen(filename,'r');
fid2 = fopen(dummyfile,'w');
linenum = 1;
k = 1;
tline = fgetl(fid1);
if linenum == 1
sfilename = tline;
linenum = linenum+1;
if tline == -1
if ~isnan(str2double(tline(1))) && k == 1
numcols = length(regexp(tline,' ','split'));
strformat = repmat('%s ',1,numcols);
floatformat = repmat('%f ',1,numcols);
k = k+1;
fid2 = fopen(dummyfile,'r');
A = textscan(fid2,floatformat);
A = cell2mat(A);
It's not an exact copy of the functionalities of the Scilab version, but since I only need the first line as a string in MATLAB, I ignore the second line with string data.
So the big question is: any hints on how I can improve this when I don't know the size of the data matrix I want to read into MATLAB (if I knew, making the formatting string would be easy, but as for now I can't see a good solution by myself).
Best regards, dm