Borrar filtros
Borrar filtros

Help with reading data from text file in Simulink and use it conduct analysis

19 visualizaciones (últimos 30 días)
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 comentario
Poseidon
Poseidon el 7 de Nov. de 2023
Ok so I was able to make readtable work with coder.extrinsic. But I am getting this error now -
Attempt to extract field 'Pressure' from 'double'
Can anyone help me with this

Iniciar sesión para comentar.

Respuestas (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 7 de Nov. de 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');
  2 comentarios
Poseidon
Poseidon el 7 de Nov. de 2023
Ok but will this work in the matlab function block? I tried readtable('MY_data.txt') and got this error - Function "readtable" not supported for code generation
Poseidon
Poseidon el 7 de Nov. de 2023
Ok so I was able to make readtable/readmatrix work with coder.extrinsic. But I am getting this error now -
Attempt to extract field 'Pressure' from 'double'

Iniciar sesión para comentar.


Poseidon
Poseidon el 7 de Nov. de 2023
Ok so I was able to figure it out. I was able to use readtable by using coder.extrinsic and defining the tables beforhand. This is one of the errors I was getting earlier. Also I defined the outputs and other variables before hand (h1 is the output here).
coder.extrinsic('readtable');
h1=zeros(1,1);
SatProp=zeros(426,6);
SubProp=zeros(101487,4);
SatProp=readtable('SatProp.txt');
SubProp=readtable('SubProp.txt');
Once I was able to read the data, the only thing I needed to change from my original matlab code is to use column numbers instead of names to call the data. For example instead of
SubProp(SubProp.Pressure==P_e_int1ow,:);
the new version is
SubProp(SubProp(:,1)==P_e_int1ow,:);
I don't know if it's possible to still use column names, if it is then I don't know how.

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by