Filter löschen
Filter löschen

Error on write to text file

6 Ansichten (letzte 30 Tage)
Tung
Tung am 26 Sep. 2011
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 Kommentare
Tung
Tung am 26 Sep. 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 am 26 Sep. 2011
If the values are true, I can connect Matlab with other languages (VB, C++) via text file to do some experiments

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Fangjun Jiang
Fangjun Jiang am 26 Sep. 2011
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 Kommentare
Walter Roberson
Walter Roberson am 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
Fangjun Jiang am 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.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 27 Sep. 2011
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 Kommentar
Tung
Tung am 28 Sep. 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);

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 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 Kommentare
Fangjun Jiang
Fangjun Jiang am 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
Tung am 27 Sep. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by