Azzera filtri
Azzera filtri

appending characters to matrix

1 visualizzazione (ultimi 30 giorni)
Tor Fredrik Hove
Tor Fredrik Hove il 24 Ott 2011
I tried to append characters to a matrix:
>> matrix=[1:3;6:8]
matrix =
1 2 3
6 7 8
>> fid=fopen('mongiss.tv', 'w')
fid =
5
>> fprintf(fid, '%d %d\n', matrix)
ans =
12
>> fclose(fid)
ans =
0
>> load mongiss.tv >> mongiss
mongiss =
1 6
2 7
3 8
>> fid=fopen('mongiss.tv', 'a')
fid =
5
>> fprintf(fid, '%d %d\n', 1, 4)
ans =
4
>> fclose(fid)
ans =
0
>> fid=fopen('mongiss.tv', 'a')
fid =
5
>> fprintf(fid, '%c %c\n', 'y', 'n')
ans =
4
>> fclose(fid)
ans =
0
>> load mongiss.tv
??? Error using ==> load
Unknown text on line number 5 of ASCII file
C:\Users\Tor\Documents\MATLAB\mongiss.tv
"y".
>>

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 24 Ott 2011
The file is written correctly. It is just that the function load() can't deal with it. If you read the help of load(). It says "With -ASCII, LOAD will error if the file is not numeric text.".
You can use importdata('mongiss.tv') to read the data. The data will be read in a structure though.
But to verify that you can append characters to a text file with numerical data, you've achieved the goal. You can use a text editor to verify it.
  1 Commento
Tor Fredrik Hove
Tor Fredrik Hove il 24 Ott 2011
Thanks for the answer! I got through it by using fgetl:
>> fid=fopen('mongiss.tv')
fid =
5
>> while ~feof(fid)
oneline=fgetl(fid)
end
oneline =
1 6
oneline =
2 7
oneline =
3 8
oneline =
1 4
oneline =
y n
>>

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by