Converting string to integer - indexing and multiple trials

36 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Askic V
Askic V il 5 Mar 2023
Modificato: Askic V il 5 Mar 2023
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
ans = 
code_numbers (1:5)
ans = 5×1
9 20 24 30 34
% check type
class(code_times)
ans = 'double'
class(code_numbers)
ans = 'double'
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)
ans = 5×1
7 750 862 923 3889
code_numbers_int(1:5)
ans = 5×1
9 20 24 30 34
If round is not what you want, then check
help floor
FLOOR Round towards minus infinity. FLOOR(X) rounds the elements of X to the nearest integers towards minus infinity. See also ROUND, CEIL, FIX. Documentation for floor doc floor Other uses of floor codistributed/floor duration/floor sym/floor datetime/floor gpuArray/floor tall/floor dlarray/floor matlab/floor
help ceil
CEIL Round towards plus infinity. CEIL(X) rounds the elements of X to the nearest integers towards infinity. See also FLOOR, ROUND, FIX. Documentation for ceil doc ceil Other uses of ceil codistributed/ceil dlarray/ceil gpuArray/ceil sym/ceil datetime/ceil duration/ceil matlab/ceil tall/ceil
  3 Commenti
Askic V
Askic V il 5 Mar 2023
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
trial1.mat trial2.mat trial3.mat trial4.mat trial5.mat trial6.mat trial7.mat trial8.mat trial9.mat trial10.mat trial11.mat trial12.mat trial13.mat trial14.mat trial15.mat trial16.mat trial17.mat trial18.mat trial19.mat trial20.mat trial21.mat trial22.mat trial23.mat trial24.mat trial25.mat trial26.mat trial27.mat trial28.mat trial29.mat trial30.mat trial31.mat trial32.mat trial33.mat trial34.mat trial35.mat trial36.mat trial37.mat trial38.mat trial39.mat trial40.mat trial41.mat trial42.mat trial43.mat trial44.mat trial45.mat trial46.mat trial47.mat trial48.mat trial49.mat trial50.mat trial51.mat trial52.mat trial53.mat trial54.mat trial55.mat trial56.mat trial57.mat trial58.mat trial59.mat trial60.mat trial61.mat trial62.mat trial63.mat trial64.mat trial65.mat trial66.mat trial67.mat trial68.mat trial69.mat trial70.mat trial71.mat trial72.mat trial73.mat trial74.mat trial75.mat trial76.mat trial77.mat trial78.mat trial79.mat trial80.mat trial81.mat trial82.mat trial83.mat trial84.mat trial85.mat trial86.mat trial87.mat trial88.mat trial89.mat trial90.mat trial91.mat trial92.mat trial93.mat trial94.mat trial95.mat trial96.mat trial97.mat trial98.mat trial99.mat trial100.mat trial101.mat trial102.mat trial103.mat trial104.mat trial105.mat trial106.mat trial107.mat trial108.mat trial109.mat trial110.mat trial111.mat trial112.mat trial113.mat trial114.mat trial115.mat trial116.mat trial117.mat trial118.mat trial119.mat trial120.mat

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by