Import data from txt file in to a 2D array

조회 수: 18 (최근 30일)
Cosimo Mercuro
Cosimo Mercuro 2021년 1월 29일
댓글: Cosimo Mercuro 2021년 1월 31일
Hi.
I've this txt file:
I'd like that all number (32) that are between the square barckets will be the first rows of a 2D array (matrix) 24x32 .
Anything like this:
Any idea?
Thanks in advance

채택된 답변

Scott Ronquist
Scott Ronquist 2021년 1월 29일
Hello Cosimo,
I understand you are trying to import the data stored in "acquisizione2-new.txt" into the MATLAB Workspace. This is possible with the Name-Value Pair Arguments available for the readtable function. After importing the data, there is one extra step that is necessary to remove NaN columns in the table output from readtable.
Please try the code given below, hope this helps!
data = readtable("acquisizione2-new.txt",'ReadVariableNames',false,...
'Delimiter',[" ","]","["], 'LineEnding', "]\n",'TrimNonNumeric',true);
% find NaN columns
nanIdx = arrayfun(@(x) all(isnan(data{:,x})), 1:width(data));
% remove NaN columns
data(:,nanIdx) = [];
% convert to numeric array (optional)
dataMatrix = data{:,:};
  댓글 수: 3
Scott Ronquist
Scott Ronquist 2021년 1월 30일
Hi Cosimo,
The output of readtable is a table variable. The values within tables can be accessd in many ways, see this summary table for more details. One of the ways to access the values of a table is to use curly brackets ("{ }"). Using curly brackets on table variables means "extract data and concatenate". Within the curly brackets you must specify 2 things: {rows, columns}. Using a colon, ":", means "all". so "{:,:}" means "extract all rows and columns and concatenate".
A more clear alternative would be to use the table2array function. Using the variables previously defined, note that the following are equivalent:
>> a = data{:,:};
>> b = table2array(data);
>> isequal(a,b)
ans =
logical
1
Glad you were able to extract your data!
Cosimo Mercuro
Cosimo Mercuro 2021년 1월 31일
Many thanks.
I'll follow your suggestions.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by