How to read values from 'Parameters' in a .ssc file?

I am making a code where I would like to read from a component called 'source.ssc', the values that are included in the parameters section. The structure of the *.ssc file kind of looks like this:
component liquid_source
nodes
%%
%%
end
annotations
%
end
parameters
p = {0.01, 'Pa'};
T = {30, 'C' };
end
variables
%
%
end
branches
%
%
end
equations
$$
end
end
My system is called filler and I have tried to call the parameters from the block with get_param('filler/source','DialogParameters') which shows me all the data, but I would like only those in the parameters section. I have read that you can use readSSCfile but my Matlab does not recognize it. I have version 2022b update 5 with simulink and simscape.

2 Kommentare

@Paola Martin: please upload a sample data file by clicking the paperclip button.
i uploaded the file, thanks for the suggestion and sorry for the delay.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 5 Apr. 2023
hello
maybe this can help you
the output is a structure (size depends of number of variables in your ssc file
here we get :
>> out(1) =
struct with fields:
name: 'p'
value: 0.0100
units: 'Pa'
>> out(2) =
struct with fields:
name: 'T'
value: 30
units: 'C'
code :
D=readlines('source.ssc'); % read as string array
ixP1=find(contains(D,'parameters')); % find the start line
ixP2=find(contains(D,'end')); % find the stop line
ixP2=ixP2(ixP2>ixP1);
ixP2=ixP2(1);
lines = ixP1+1:ixP2-1; % number lines
for ck=1:numel(lines)
tmp = D(lines(ck));
out(ck).name = char(strtrim(extractBefore(tmp,'=')));
out(ck).value = str2double(extractBetween(tmp,'{',','));
out(ck).units = char(strtrim(extractBetween(tmp,'''','''')));
end

3 Kommentare

This should work even with blank lines (at least it works on my side with and without blank lines)
attached both versions of the dummy ssc files
D=readlines('source.ssc'); % read as string array
ixP1=find(contains(D,'parameters')); % find the start line
ixP2=find(contains(D,'end')); % find the stop line
ixP2=ixP2(ixP2>ixP1);
ixP2=ixP2(1);
lines = ixP1+1:ixP2-1; % number lines
k = 0;
for ck=1:numel(lines)
tmp = char(D(lines(ck)));
% check if line is empty or not
if ~isempty(tmp)
k = k+1;
out(k).name = strtrim(extractBefore(tmp,'='));
out(k).value = str2double(extractBetween(tmp,'{',','));
out(k).units = strtrim(extractBetween(tmp,'''',''''));
end
end
It worked, thanks. What I didn't notice is that the file has two 'parameters' sections. Also is it possible to read the comments in the file or should I open another section to ask?
hello
I can work on this without any extra charge :)
but maybe the file structure I used in the previous code is missing that second parameter section and also the comments section
how should the new *.ssc file look like then ?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Foundation and Custom Domains finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by