Converting string to integer - indexing and multiple trials
38 ビュー (過去 30 日間)
古いコメントを表示
I have 120 trials in one Matlab file, all trials are separate within the file and look similar to that of trial1.mat (attached). I am importing this Matlab file into Python and indexing into the correct spot with
cue_times = trial(['BehavioralCodes']['CodeTimes'], dtype=int)
**I know this is not a Python platform, and that is not my question, I am merely explaining what I'm doing when I get the error I am getting.**
I get the error that the list indices must be integers or slices, not str
I need to convert 'CodeTimes' within 'BehavioralCodes' in each trial (each trial is saved individually like trial1) from a string to an integer.
I have tried:
code_times = str2num('CodeTimes') but that gives me an empty array so I am definitely doing something wrong.
I am not sure how to index into the correct space and then to include all trials (perhaps a for loop?). Any help would be greatly appreciated!
0 件のコメント
採用された回答
Askic V
2023 年 3 月 5 日
編集済み: Askic V
2023 年 3 月 5 日
If you look into your data in Matlab, you will see that CodeTimes is actually an array of doubles and not strings:
cc = load (websave('trial1.mat', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1315225/trial1.mat'));
code_times = cc.Trial1.BehavioralCodes.CodeTimes;
code_numbers = cc.Trial1.BehavioralCodes.CodeNumbers;
vpa(code_times(1:5),6) % show first 5 elements
code_numbers (1:5)
% check type
class(code_times)
class(code_numbers)
In Matlab you can use function round to round to the neareast integer:
code_times_int = round(code_times);
code_numbers_int = round(code_numbers);
% show first 5 elements
code_times_int(1:5)
code_numbers_int(1:5)
If round is not what you want, then check
help floor
help ceil
3 件のコメント
Askic V
2023 年 3 月 5 日
This loop will give you an answer (assuimg the name is trialX.mat)
for i = 1:120 % assuming 120 files in total
filename = ['trial', num2str(i), '.mat']; % form filename
% load filename and perform processing like already described
fprintf('%s\n',filename)
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!