How do i read the exact values from a textfile

I have a textfile that contains a vector of length 441. However when i use the fileread command, Matlab says the file has length.12350. It prints put all the values but a wrong size. How do I correct this?
The file ia attached below

5 comentarios

Deepak Gupta
Deepak Gupta el 28 de Abr. de 2020
Include your text file.
Geoff Hayes
Geoff Hayes el 28 de Abr. de 2020
SA - what happens if you use importdata instead? fileread does the same for me (creating an array of size 1x12350) because it is a character array and not the numeric array that you are expecting.
SA
SA el 28 de Abr. de 2020
Hi Geoff, I get an empty array when I use importdata.
I want to read just the 441 double data in the file.
Geoff Hayes
Geoff Hayes el 28 de Abr. de 2020
SA - strange..not sure why it would be empty. Possibly something in the formatting of the file (?). See Tommy's answer below for a working solution.
SA
SA el 29 de Abr. de 2020
Hi Geoff, I used the importdata and it worked. I am just wondering why it didn't at first. Thanks for the help

Iniciar sesión para comentar.

Respuestas (1)

Tommy
Tommy el 28 de Abr. de 2020
Editada: Tommy el 28 de Abr. de 2020
fileread is reading each character from the file. If you want to interpret those characters as numbers, one option is to use textscan:
fileID = fopen('finite1_lu.txt');
A = textscan(fileID, '%f', 'HeaderLines', 1);
fclose(fileID);

8 comentarios

SA
SA el 28 de Abr. de 2020
I just want the 441 double data in the file
Tommy
Tommy el 28 de Abr. de 2020
A 441x1 double array should be stored in A{1}.
SA
SA el 28 de Abr. de 2020
Hi Tommy,
I don't understand
Tommy
Tommy el 28 de Abr. de 2020
Have you run the code in my answer? What results did you get?
SA
SA el 28 de Abr. de 2020
I did ran your code and I had a 0 by 1 double array
Hmm. Any luck with readmatrix?
A = readmatrix('finite1_lu.txt', 'NumHeaderLines', 1, 'ExpectedNumVariables', 1);
SA
SA el 29 de Abr. de 2020
It says the readmatrix is an undefined name
SA
SA el 29 de Abr. de 2020
I was able to solve it. I used the importdata and it worked. Thanks for the assistance

Iniciar sesión para comentar.

Categorías

Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.

Preguntada:

SA
el 28 de Abr. de 2020

Comentada:

SA
el 29 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by