Inputing textfile data as meaningful variables

조회 수: 1 (최근 30일)
Catherine McAlister
Catherine McAlister 2020년 4월 26일
편집: per isakson 2020년 4월 26일
Hi!
I'm having a bit of trouble inputing a collection of large data as a textfile and turning that data into meaningful variables.
My code currently only returns the first value of that column of values that I am interested in, hoping for it to instead create a cell array of all values in that first column.
Probably an easy solution, still figuring out MATLAB, any help would be appreciated. Thank you:)
% Load Data
file = fopen('data.test.txt','r');
str = textscan(file,'%f%s%f%s%f%s%s%s%s%s%f%f%f%s');
fclose(file)
% Data Variables
Age = str{1};
  댓글 수: 1
Image Analyst
Image Analyst 2020년 4월 26일
You forgot to attach 'data.test.txt'. In the meantime, try importdata() or readmatrix().

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2020년 4월 26일
편집: per isakson 2020년 4월 26일
Matlab provides several different functions to read text files. (I'm used to textscan.)
"cell array of all values in that first column" Your format string shows that the first column contains numerical data. I guess that your textscan statement fails because there is an error in your format string. (It's easy to get the number and mix of columns wrong.)
Here is a code that avoids the trouble of counting columns
%%
fid = fopen('data.test.txt','rt');
str = textscan( fid, '%f%*[^\n]' ); % Read the first column and skip to the end of line
[~] = fclose( fid );

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by