필터 지우기
필터 지우기

Error on write to text file

조회 수: 6 (최근 30일)
Tung
Tung 2011년 9월 26일
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
Tung
Tung 2011년 9월 26일
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 2011년 9월 26일
If the values are true, I can connect Matlab with other languages (VB, C++) via text file to do some experiments

댓글을 달려면 로그인하십시오.

답변 (3개)

Fangjun Jiang
Fangjun Jiang 2011년 9월 26일
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
Walter Roberson
Walter Roberson 2011년 9월 28일
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 2011년 9월 30일
@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
Jan 2011년 9월 27일
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
Tung
Tung 2011년 9월 28일
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);

댓글을 달려면 로그인하십시오.


Jan
Jan 2011년 9월 26일
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
Fangjun Jiang
Fangjun Jiang 2011년 9월 26일
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 2011년 9월 27일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by