write an array (or other data) to a file in matlab
February 15th, 2006 by Lawrence David
[update: a much easier solution]save ‘my_file.name’ my_array_or_matrix -ASCII -TABS
to write a file in matlab, you first need to get a file id:
>> fid = fopen(‘nameofyourfile.txt’,'w’)
(the ‘w’ means that this file will completely overwrite whatever file shares the same filename)
to write to the file, use the fprint command:
>> fprintf(fid,’%d’,5);
this puts the number 5 into nameofyourfile.txt.
to prevent the file handling gnomes from attacking you in the night, be sure to close the file when you’re finished writing all of your data:
>> fclose(fid);
hi there,
isn’t it supposed to be fprintf(fid,’%d’,5); ?
greetings
why yes it is! thanks deine! this post has been duly updated.
OOPS
you’re instructions are wrong, good sir.
>> fprintf(fid,’%d’,5);
??? fprintf(fid,’%d’,5);
|
Error: The input character is not valid in MATLAB statements or expressions.
Whats the difference between the statements? They all look the same to me. Am I missing something?
hi trevor — you might want to check that when you copy and paste, you’re using single quotes, instead of backticks!
this is not really writing an array to a file. it is writing one number into a file. How do you handle the command if you’ve got an array containing cells with different formats?
yes, how is it about vectors?
thansk a lot..
those help docs at matlab servers suck…
everthings so irrelevant
And how to write one element of array to a single line.
ex A=[1 2 3 4]
=>
1
2
3
4
Hi,
I have same problem a bit different.
I have a m-file, I want to delete a special row in it and replace it with another statement through matlab command window ,(for example in my m-file there is p=2; in 7th row, and I want to delete it and replace with p=11.234;).
What is the easiest and best way to do so?
Thanks,