Error on write to text file

Hi everyone
I want to output the simulink results to a text file during running simulation. But the values of the text file are different with the true values (compare with the values in the Workspace). I also use Bock to export the result to a mat file and compare the result with the text file: The values in the mat file are the same as the values in the Workspace. I also know that this is the true values. But the values in the text file are different. For example the value in the workspace and mat file is 0.9913, but in text file is 1.012. But infact, the true values do not exceed 1. While the simulation, the values in the text file are always bigger than actual values. Please help me.

4 Commenti

TAB
TAB il 26 Set 2011
How you are exporting simulink data to text file?
Fangjun Jiang
Fangjun Jiang il 26 Set 2011
Good experiment! See http://www.mathworks.com/matlabcentral/answers/15982-how-to-store-output-data-from-a-simulink-model-to-a-text-file.
Post your code so we can figure it out. I am interested too. Just want to see if it's possible.
Tung
Tung il 26 Set 2011
In the output of the simulink, I used S-function with code is
function [sys,x0,str,ts] = sfwritetext(t,x,u,flag)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
% Initialize the states, sample times, and state ordering strings.
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
% Return the outputs of the S-function block.
case 3
sys=mdlOutputs(t,x,u);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 1, 2, 4, 9 }
sys=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unexpected flags (error handling)%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Return an error message for unhandled flag values.
otherwise
error(['Unhandled flag=', num2str(flag)]);
end;
% end timestwo
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1; % dynamically sized
sizes.NumInputs = 1; % dynamically sized
sizes.DirFeedthrough = 1; % has direct feedthrough
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
x0 = [];
ts = [-1 0]; % inherited sample time
% end mdlInitializeSizes
%
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%
function sys = mdlOutputs(t,x,u)
fid=fopen('value.txt','w');
sys=fprintf(fid,'%f',u);
fclose(fid);
Tung
Tung il 26 Set 2011
If the values are true, I can connect Matlab with other languages (VB, C++) via text file to do some experiments

Accedi per commentare.

Risposte (3)

Fangjun Jiang
Fangjun Jiang il 26 Set 2011

1 voto

I just confirmed that with fopen(File,'w'), the content of the file will be over-written. So you will only get the last value, which is not intended.
There might be a way to not to run fopen() and fclose() at every time step. Bu the easiest way is to write the text file in appending mode. Use fopen(File,'at').

16 Commenti

Tung
Tung il 27 Set 2011
Over-written is my purpose. Because when running simulation, the other program read the values in text file continuously and transmit to real machine for control.
I used fopen(file, 'a+') or frintf(1,'%f',u) to output the results into screen to see. But the result is the same as I said before.
I also though as you said that "approach will require hard drive access at every time step, maybe even open and close the file at every time step" but in this way, the values must be always smaller than the values in the workspace. Because the writing values in the text file is always slower than the current values in the output of simulink.
Try writing the values in binary with fwrite(); you can convert them to text in another pass.
Fangjun Jiang
Fangjun Jiang il 27 Set 2011
@Tung, It shouldn't happen that way.
Make the input to your S-function a Constant, for example, 1.
Write the constant value to your text file, over-writing or appending, check the modified date and time of the text file to make sure it is the latest updated file.
Also fprintf(1,'%f\n',u) to the Command Window. Are you saying the value shown in Command Window is incorrect?
Tung
Tung il 27 Set 2011
If I input to my S-function a Constant, It will over-writing the previous value. The latest update value is true. That is the same with fprintf(1,'%f\n',u) command. This is always true with the static computation. Here is the dynamic computation.
I found the reason for wrong values is that the values are written by S-funtion (fprintf() command) are too more than in workspace or mat file. For example, the sample time is 0.01sec, so if the simulink runs during 0.1sec, we will have 11 values:
The true result (workspace, mat file):
0 0.0323669344980132 0.0657510988130542 0.100519139757348 0.134300252466615 0.161020802808427 0.189548037935473 0.217621290771149 0.245709502694545 0.272320032018139 0.299432452531348
But the result when using fprintf() command will be:
0.000000 0.000000 0.001569 0.008778 -0.097553 -0.165451 0.032367 0.039412 0.043499 0.052341 0.062057 0.089179 0.065751 0.073395 0.077500 0.082114 0.059340 0.066505 0.100519 0.106275 0.108107 0.155264 0.217676 0.212041 0.134300 0.141728 0.144117 0.145211 0.107391 0.097592 0.161021 0.165293 0.167748 0.197785 0.238145 0.243443 0.189548 0.196110 0.199292 0.201392 0.176475 0.179310 0.217621 0.222631 0.225193 0.247602 0.269238 0.272540 0.245710 0.251596 0.253537 0.263359 0.235320 0.216378 0.272320 0.277122 0.279697 0.300493 0.320633 0.325022 0.299432
I don't know, what is wrong when output text file in this way
Fangjun Jiang
Fangjun Jiang il 27 Set 2011
Good finding, Tung! I suggest you asking a separate question to see if any Mathworker can give any insight. Or Contact the Mathworks tech support if this is critical to you!
I notice that your model has dynamically sized outputs. Is there a way to determine whether the model has switched to having 6 outputs? You are getting 6 values printed for each 1 that you are expecting. You might want to disp(size(u)) to help debug that.
Fangjun Jiang
Fangjun Jiang il 27 Set 2011
That is a good observation, Walter! I have that questions too. Why there is no many more data? I though it might be minor time step. Can you simplify your model to use fixed discrete time step, Tung?
Fangjun Jiang
Fangjun Jiang il 27 Set 2011
It seems that every 6th element in the text file matches the true value.
>> NewValue=TextValue(1:6:end);
[TrueValue;NewValue]'
ans =
0 0
0.0324 0.0324
0.0658 0.0658
0.1005 0.1005
0.1343 0.1343
0.1610 0.1610
0.1895 0.1895
0.2176 0.2176
0.2457 0.2457
0.2723 0.2723
0.2994 0.2994
Jan
Jan il 27 Set 2011
@Fangjun: This will be the key to solve the problem. The u in the fprintf ontains more data. It would be easy to find this by using the debugger.
Tung
Tung il 28 Set 2011
@Fangjun Jiang
As I said when I post the values that I used sample time 0.01 (fixed-step). And the time for simulation from 0 to 0.1sec.
Tung
Tung il 28 Set 2011
@Jan Simon
If I write all data from start to stop simulation, it is not difficult to find true values as Fangjun found. But here, the value is updated and over-writing previous value. Therefore, in this way, how to force the fprintf() command write value in the text file after every 6 output values of simulink? Please help me.
The other thing which I don't understand is that I setup the fixed-step, therefore the output values will has 11 values only why there are too more than 11 values?
Fangjun Jiang
Fangjun Jiang il 28 Set 2011
@Tung, that is the unknown that I am eager to find out. I think you are at the best position to find it out for us.
Try to still write the file in appending mode. Just run the simulation for a short period of time so the data won't be overwhelming.
Walter mentioned that u could be a vector input, did you check that?
When I say "fixed step", I mean the type of the solver is "fixed step" and the solver is "discrete". Is it possible for you to select "discrete" solver?
Use fprintf(1,...) at the same time? Does the Command Window show 11 data points, or 66 data points?
Tung
Tung il 28 Set 2011
Thank you for your finding and comments. Now, I am finding the method for solving that problem. If I find out I will post here.
If write the file in appending mode, before transfer the values to machine, I can use other language (C, VB) for choosing a true values in the file. But I want to solve this problem by using Matlab, that means in the text file has only true value, and each instant time has only one value in the text file. It will be easy for next processing.
u is one value in instant time. I use in this way
The model is continuous state so cannot choose the solver is "discrete"
I use fprintf() in two separate time, and both of them showed 66 data points
Fangjun Jiang
Fangjun Jiang il 28 Set 2011
Okay, that is good information. You know that even for "fixed" time step, Simulink solver might take "minor time step". Can you try to change the solver from ode45 to ode23 to see if different number of data points are written?
You believe that u is one value at any time point, but did you use disp() or put in a break-point to cross-check that ?
Fangjun Jiang
Fangjun Jiang il 30 Set 2011
@Tung, any update? Another suggestion is to set the sample time to be 0.01 instead of inherit. I think that will help to get to the bottom of this problem.

Accedi per commentare.

Jan
Jan il 27 Set 2011

1 voto

Your code does not insert spaces after writing a number: "sys=fprintf(fid,'%f',u)". But your data contain spaces: "0.000000 0.000000 0.001569 ...". Either you did not post the original code or your run another program.
Please set a breakpoint in the FPRINTF line to find out, what's going on.

1 Commento

Tung
Tung il 28 Set 2011
Thank you for your observation. For receiving above values, the code are
function sys = mdlOutputs(t,x,u)
fid=fopen('value.txt','a+');
sys=fprintf(fid,'%f\t',u);
fclose(fid);
or
function sys = mdlOutputs(t,x,u)
sys=fprintf(1,'%f\t',u);

Accedi per commentare.

Jan
Jan il 26 Set 2011
How do you create the text file? It seems to be obvious that there is a bug in this routine.
[EDITED] after reading your comment showing the code:
FPRINTF works correctly. So either you do not wnat u but x, or you write to a file in the current folder, but this is not the folder you are expecting. Then the file with the "wrong" values was written by an earlier version.
Better add the path to the output file and check the success of FOPEN in every case:
fid = fopen(fullfile(tempdir, 'value.txt'), 'w');
if fid < 0, error('Cannot open file'); end

2 Commenti

Fangjun Jiang
Fangjun Jiang il 26 Set 2011
Also, try fopen(fullfile(tempdir, 'value.txt'), 'at'), which means writing text file and appending it. I am not sure if 'w' alone will overwrite the previous file.
Tung
Tung il 27 Set 2011
After re-considering the S-function, I think the output is u. I placed code of S-function in the same folder with simulink file. I also deleted the "value.txt" and run the simulation again. The result is also the same as before.
So, I don't know what is wrong in my program

Accedi per commentare.

Categorie

Scopri di più su General Applications in Centro assistenza e File Exchange

Richiesto:

il 26 Set 2011

Community Treasure Hunt

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

Start Hunting!

Translated by