(HELP) Assign char values from a structure to a double column vector

7 ビュー (過去 30 日間)
Ana Maldonado Gómez
Ana Maldonado Gómez 2021 年 5 月 11 日
Hi, everyone! :)
I hope you all are doing well.
I wanted to ask for help, since I am struggling a bit with a for loop.
I have this for loop:
AllEvents=[];
AllEvents=zeros(length(EEG.event),1);
for i=1:length(EEG.event)
AllEvents(i,1)=EEG.event(i).type;
end
However, every time I try to run it, I get the following error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-3.
The values of the variables (and/or structures, sorry I am a bit inexperienced) are:
  1. AllEvents = 1244x1 double
  2. EEG.event = 1 x 2444 struct array with fields: type, edftype, latency, urevent
  3. i (before running the loop) = 1x1244 double
  4. Mango (extra variable that I used to see the size of the EEG.event(i).type) = 1x3 char.
So far, I think the purpose of the loop is to assign the values of the EEG.event(i).type to the all events column vector, however I think it is affecting that EEG.event is a structure and the "type" field contains characters (they look like strings).
I was wondering, and I'd be truly grateful if someone could guide me with this (please).
Thank you for everything in advance.

採用された回答

Madhav Thakker
Madhav Thakker 2021 年 5 月 20 日
編集済み: Madhav Thakker 2021 年 5 月 20 日
Hi Ana,
The error message clearly says the problem - Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-3.
AllEvents(i,1) is of size 1-by-1 and EEG.event(i).type is of size 1-by-3.
AllEvents(i,1)=EEG.event(i).type;
Can you try with something like this -
AllEvents=[];
AllEvents=char(length(EEG.event),3); % Changed the type and size so that each row is char array of size 1-by-3
for i=1:length(EEG.event)
AllEvents(i)=EEG.event(i).type; % assigning the type value to each row
end
  2 件のコメント
Stephen23
Stephen23 2021 年 5 月 20 日
編集済み: Stephen23 2021 年 5 月 20 日
AllEvents(i,:) = EEG.event(i).type;
% ^^^ Correct indexing for assigning the 1x3 char vector to each row.
But this will fail as soon as you have a <type> character vector with two, four, five, or greater number of characters. I.e. this answer is not robust. If you want to write robust code, either use a cell array or a string array.
Ana Maldonado Gómez
Ana Maldonado Gómez 2021 年 6 月 17 日
Thank you so much! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by