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 Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by