appending characters to matrix

1 visualización (últimos 30 días)
Tor Fredrik Hove
Tor Fredrik Hove el 24 de Oct. de 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".
>>

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 24 de Oct. de 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 comentario
Tor Fredrik Hove
Tor Fredrik Hove el 24 de Oct. de 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
>>

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by