Help with reading data from text file in Simulink and use it conduct analysis
Ältere Kommentare anzeigen
Ok this is a long one.
I have a MATLAB code where I read data from the text file and use that data to conduct some analysis. The text file has 6 columns and each column has a label. In the code, I have to use data from a particular column and use interpolation to find values. Some of the lines that I use in this MATLAB code are-
SubProp_e_intlow=SubProp(SubProp.Pressure==P_e_int1ow,:);
h1=interp1(SatProp.Pressure,SatProp.LiqEnth,P_cond);
(SubProp and Sat Prop are the tables loaded from the text files, pressure is the column in both the tables. First line is used to seperate the table into smaller tables depending on the working pressure, to further use interpolation function on these smaller tables)
This code works perfectly fine.
My problem is trying to make this work in Simulink. I have to create a matlab function block and use this code or similar logic. There are several problems that I ran into:
- My first problem was reading the data into the matlab function block. I don't know how to read from a text file. Function readtable doesn't work directly, so I include code.extrinsic(readtable), but this didn't work and I ran into many problems.
- Then I tried converting txt data file to csv file and use 'from file' block to read this data. But this divided the columns into seperate tables/outputs (I think), so I had trouble using the same interp1 function as above. So I tried something else in a test code, which you can see in the picture below
.

where the code in matlab function is
function [P_L_intlow,P_L_intup,hi] = fcn(P_H,P_L,a,b,c,d,e,f)
P_H=P_H;
P_L=P_L;
hi=zeros(1,1);
P_H_int1ow=floor((P_H)/10000)*10000;
P_H_intup=ceil((P_H)/10000)*10000;
if P_H_int1ow==0
P_H_int1ow=1;
end
P_L_intlow=floor((P_L)/10000)*10000;
P_L_intup=ceil((P_L)/10000)*10000;
if P_L_intlow==0
P_L_intlow=1;
end
i=find(a==P_L_intlow);
j=find(a==P_L_intup);
hi=c(i,1);
hj=c(j,1);
h1=(((hj-hi)/(P_L_intup-P_L_intlow))*(P_L-P_L_intlow))+hi;
Here the error I am getting is related hi - Data 'hi' is inferred as a variable size matrix, while its properties in the Model Explorer specify its size as inherited or fixed. Please check the 'Variable Size' check box and specify the upper bounds in the size field.
So I change it to variable size and I am getting error saying there is Size Mismatch.
Honestly, I don't know how to approach this. I want to be able to read the data into the MATLAB function all at once, and then use that data in the same way I used in the MATLAB code.
Any help would be appreciated.
1 Kommentar
Poseidon
am 7 Nov. 2023
Antworten (2)
Sulaymon Eshkabilov
am 7 Nov. 2023
There are a few ways it can be attained. One of the quick and easy way, to import a data from the *.txt file in matlab and read the data in your Simulink model from the workspace.
D = readmatrix('MY_data.txt');
Poseidon
am 7 Nov. 2023
Kategorien
Mehr zu String finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!