2 views (last 30 days)
KatherineS on 6 Jun 2019
Edited: KatherineS on 7 Jun 2019
Hello, I am trying to get MATLAB to read this file and to import all the values as variables and ignore the lines starting with # and \$. However everything I try fails to do this.
#--------------------------------------------------
\$ 1 Simulation parameters
x = t45 # id
y = 20120907 # random number
z = 0.0 # time / s
...
#--------------------------------------------------
\$ 2 Orbit parameters
# inc = 0.001 # inclination
# ecc = 0.01 # eccentricity
...
I have tried the code below and many other methods.
fid = fopen('file', 'rt');
filepos = 0;
tline = fgetl(fid);
while ~isempty(tline) && any(strncmp(tline, {'#', '\$'}))
filepos = ftell(fid);
tline = fgetl(fid);
end
fseek(fid, filepos, 'bof');
C = textread(fid, '%s' , 'Delimiter', ';');
fclose(fid);

Jan on 6 Jun 2019
Edited: Jan on 6 Jun 2019
C = strsplit(S, char(10));
C(strncmp(C, '\$'), 1) = [];
C(strncmp(C, '#'), 1) = [];
C = strtok(C, '#'); % remove trailing comments
Data = struct([]);
for iC = 1:numel(iC)
L = strsplit(C{iC}, '=');
Data.(L{1}) = L{2};
end
Maybe you want to convert everything, which looks like a number to a number?
Data = struct([]);
for iC = 1:numel(iC)
L = strsplit(C{iC}, '=');
Value = str2double(L{2});
if isnan(Value) % No valid convertsion to a number
Data.(L{1}) = L{2};
else
Data.(L{1}) = Value;
end
end

#### 1 Comment

KatherineS on 7 Jun 2019
Thank you!