Error on write to text file
Show older comments
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 Comments
TAB
on 26 Sep 2011
How you are exporting simulink data to text file?
Fangjun Jiang
on 26 Sep 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
on 26 Sep 2011
Tung
on 26 Sep 2011
Answers (3)
Fangjun Jiang
on 26 Sep 2011
1 vote
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 Comments
Tung
on 27 Sep 2011
Walter Roberson
on 27 Sep 2011
Try writing the values in binary with fwrite(); you can convert them to text in another pass.
Fangjun Jiang
on 27 Sep 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
on 27 Sep 2011
Fangjun Jiang
on 27 Sep 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!
Walter Roberson
on 27 Sep 2011
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
on 27 Sep 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
on 27 Sep 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
on 27 Sep 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
on 28 Sep 2011
Tung
on 28 Sep 2011
Fangjun Jiang
on 28 Sep 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
on 28 Sep 2011
Fangjun Jiang
on 28 Sep 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?
Walter Roberson
on 28 Sep 2011
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
on 30 Sep 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.
Jan
on 27 Sep 2011
1 vote
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.
Jan
on 26 Sep 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 Comments
Fangjun Jiang
on 26 Sep 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
on 27 Sep 2011
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!