how to Vectorize this for loop

조회 수: 2 (최근 30일)
Milad Ghobadi
Milad Ghobadi 2020년 2월 13일
댓글: Milad Ghobadi 2020년 2월 17일
Hi everyone i have wrote this code and i want to vectorize my code so it run faster here is the part of code i'm talking about
fileID = fopen('CANDec(day_1_9).txt','r');
text_data = textscan(fileID,'%s %s %s %s %s %s %s %s',1000,'delimiter',';');
hex=text_data(:,4);
signal_name=text_data(:,7);
decimal_wert=text_data(:,8);
for i =1:length(text_data{1,1})
can.(signal_name{1,1}{i,1}).hex{i,1}=hex{1,1}{i,1};
can.(signal_name{1,1}{i,1}).decimal_wert{i,1}=decimal_wert{1,1}{i,1};
end
hex , signal_name and decimal_wert are cells that i read from a .txt file (CANDec(day_1_9)). it has actually many million lines . it's means there is millions of signal_name but here i read just 1000 lines.
now i'm building a structure can and as the name for substructures i use signal_name dynamically. if you go in can-structure you see all signal_name as substructure and in these substruct you find again 2 substruct with the names hex and decimal_wer.
i hope you understand the code . so my goal is to vectorize this for loop . when i remove the for and vectorize it simply I will get this error :
Expected one output from a curly brace or dot indexing expression, but there were 1000 results.
i =1:length(text_data{1,1})
can.(signal_name{1,1}{i,1}).hex{i,1}=hex{1,1}{i,1};
can.(signal_name{1,1}{i,1}).decimal_wert{i,1}=decimal_wert{1,1}{i,1};
i hope you can help me with that.
Thank you

채택된 답변

Stephen23
Stephen23 2020년 2월 13일
편집: Stephen23 2020년 2월 13일
Try doing something like this:
[fid,msg] = fopen('CANDec(day_1_9).txt','rt');
assert(fid>=3,msg) % good practice to check the FID.
C = textscan(fileID,'%s%s%s%s%s%s%s%s',1000,'delimiter',';');
fclose(fid);
% Simpler to get the CONTENTS of the cell array:
hx = C{4}; % hex
sn = C{7}; % signal name
dw = C{8}; % decimal wert
I strongly recommend that you do not use nested structures, but instead store the signal name as data in its own right in a non-scalar structure, then your task is trivial:
can = struct('signalName',sn, 'hex',hx, 'decimalWert',dw)
And you can access the data in can using some very convenient sytaxes:
  댓글 수: 5
Stephen23
Stephen23 2020년 2월 14일
편집: Stephen23 2020년 2월 14일
sn = { 'B_UF'; 'B_IN'; 'B_UF'; 'B_UF'; 'B_STMAX'; 'B_STMAX'};
hx = { '374C'; '025A'; '00'; 'BB'; 'AA'; 'F9'};
dw = {'704.17';'29.94'; '0'; '1234'; '0'; '249.0'};
[fn,~,ix] = unique(sn);
Using nested structures:
can = struct();
for k = 1:max(ix)
can.(fn{k}).hex = hx(k==ix);
can.(fn{k}).dWt = dw(k==ix);
end
Checking:
>> can.B_UF.hex
ans =
'374C'
'00'
'BB'
Or with a non-scalar structure:
can = struct();
for k = 1:max(ix)
can(k).name = fn{k};
can(k).hex = hx(k==ix);
can(k).dWt = dw(k==ix);
end
Checking:
>> can(3).name
ans =
B_UF
>> can(3).hex
ans =
'374C'
'00'
'BB'
I strongly recommend that you use a non-scalar structure. As well as the ease of access, consider what your code would do if one of the signal_names is not a valid fieldname.
Milad Ghobadi
Milad Ghobadi 2020년 2월 17일
Oh of coure old fashioned unique I almost forget about it . Great Thanks alot

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by