필터 지우기
필터 지우기

How can I extract arrays from a structure?

조회 수: 2 (최근 30일)
Borja
Borja 2013년 5월 10일
Hi all,
This is my problem:
I load several files using the command 'dir'. Then, I import the data sheet (numbers + headers) with the command 'importdata'. The result is a structure (number of objects imported x 1).
The data are matrices of (602,603 or 604 x 9). I want to extract certain columns of the data sheets and I use a for loop but it gives this error:
Subscripted assignment dimension mismatch.
Error in loading (line 12)
Q(length(vals(i).data(:,7)),i)= vals(i).data(:,7)
Here I attach the piece of code:
clc;clear all;
% Loads the files of the selected directory
loadfiles = dir('*.txt');
% Preallocation of the variable which will contain the data
vals = [];
Q = [];
for i = 1:length(loadfiles)
% Select the "useful" part of the structure (data)
vals = [vals; importdata(loadfiles(i).name)];
% Select the seventh column of each data sheet
Q(length(vals(i).data(:,7)),i)= vals(i).data(:,7)
end
thanks in advance

답변 (2개)

Alessandro Renna
Alessandro Renna 2013년 5월 11일
편집: Alessandro Renna 2013년 5월 11일
try to use cell variable for Q as:
Q{length(vals(i).data(:,7)),i}= vals(i).data(:,7)

Cedric
Cedric 2013년 5월 12일
편집: Cedric 2013년 5월 12일
There is no such thing as vals(i).data.
If you wanted to save only the 7 relevant columns, you could update a little your code as follows:
cId = [1 2 3 4 6 7 9] ; % ID of the 7 relevant columns.
Q = [] ;
for i = 1 : length(loadfiles)
buffer = importdata(loadfiles(i).name) ; % Temporary storage.
Q = [Q; buffer.data(:,cId)] ; % Append only 7 relevant cols.
end

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by