number of columns on line 1 of ascii file must be the same as previous lines

2 views (last 30 days)
Hi guys,
I'm trying to load a variable of my main function in my subfunction and update it as here:
sub:
function [] = myfunction(q)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fid = fopen('c:\\coeffs.txt','wt'); % Note the 'wt' for writing in text mode
fprintf(fid,'%f',q(i,:)); % The format string is applied to each element of a
fclose(fid);
i=i+1;
end
where 'i' is a variable of main. But I'm getting the error described above. Could you gyus helpe me out?
Thamk you

Accepted Answer

Image Analyst
Image Analyst on 6 Apr 2014
In the function definition, you need to pass in i:
function myfunction(q, i)
Then also pass in i when you call it.
myfunction(q, i);
  4 Comments
Carlos Ramada
Carlos Ramada on 6 Apr 2014
Edited: Image Analyst on 6 Apr 2014
Hi fellow,
I tried to use setappdata() as you said but it only works to update a Handle structure. In my case I want to update the value of 'i' in another script(.m file).
Using the other option,global, I tried to make this:
function [] = myfunction(~)
global d
function [] = myfunction(q,d)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
fid = fopen('c:\\coeffs.txt','wt');
fprintf(fid,'%f',q(d,:));
fclose(fid);
d=d+1;
end
end
So, I tried to use a nested function but I got the following error:
??? Error while evaluating TimerFcn for timer 'timer-10'
Error: Unbalanced or unexpected parenthesis or bracket.
I can't find where is that damn unbalanced missing element.
Do you know what could have caused this?
Image Analyst
Image Analyst on 6 Apr 2014
Not sure if the backslash is messing it up. Try forward slash, which works just fine with Windows, or a single backslash:
fopen('c:/coeffs.txt','wt'); % or...
fopen('c:\coeffs.txt','wt');

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!