How can I use this code for stabilogram diffusion analysis (SDA)?

clear; clc; close;
%% SDA Analysis
% Use this file to calculate the SDA parameters for the High- and Low-CMP
% condition. Results will be stored in the 'Results' variables (either
% _HighCMP or _LowCMP).
% These files contain the following outcomes:
% Column 1 - Mediolateral critical time
% Column 2 - Mediolateral critical value
% Column 3 - Mediolateral short-term diffussion coefficient
% Column 4 - Mediolateral long-term diffussion coefficient
% Column 5 - Anterior-posterior critical time
% Column 6 - Anterior-posterior critical value
% Column 7 - Anterior-posterior short-term diffussion coefficient
% Column 8 - Anterior-posterior long-term diffussion coefficient
%% Specify data path locations (of input and output) here:
% Input
datapatho='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\';
% Output
datapathc='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\6r1cop.csv\';
%% Low-CMP condition
condition = 1;
Files=char('6r1cop.csv');
for i = 1:length(Files)
datafiles=strtrim(Files(i,:));
[param_E(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_LowCMP = param_E';
%% High-CMP Condition
condition = 2;
Files=char('6r1cop.csv');
for i = 1:length(Files)
datafiles=strtrim(Files(i,:));
[param_I(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_HighCMP = param_I';
%%

Answers (1)

Files=char('6r1cop.csv');
That is a 1 x 10 character vector.
for i = 1:length(Files)
length(VARIABLE) is defined as:
take size() of variable
if any of the dimensions were 0, length is 0
otherwise length is max() of the dimensions
so for the 1 x 10 character vector, length() is 10.
datafiles=strtrim(Files(i,:));
You are trying to use up to 10 as the row number, but there is only one row.
If you want i to only be valid row numbers, use size(Files,1) or (newer MATLAB) height(Files)
% Input
datapatho='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\';
% Output
datapathc='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\6r1cop.csv\';
is 6r1cop.csv a file inside C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\ or is it a folder in that location?

10 Comments

Thanks for the reply.
I would like to flow this code with data from a gravimetric sway meter.
I changed from the original code to this.
Here is the original code.
clear; clc; close;%% SDA Analysis% Use this file to calculate the SDA parameters for the High- and Low-CMP % condition. Results will be stored in the 'Results' variables (either% _HighCMP or _LowCMP).% These files contain the following outcomes:% Column 1 - Mediolateral critical time% Column 2 - Mediolateral critical value% Column 3 - Mediolateral short-term diffussion coefficient% Column 4 - Mediolateral long-term diffussion coefficient% Column 5 - Anterior-posterior critical time% Column 6 - Anterior-posterior critical value% Column 7 - Anterior-posterior short-term diffussion coefficient% Column 8 - Anterior-posterior long-term diffussion coefficient%% Specify data path locations (of input and output) here:% Inputdatapatho='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Input\';% Outputdatapathc='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Output\';%% Low-CMP conditioncondition = 1;Files=char('P3_E.mat','P4_E.mat','P7_E.mat','P8_E.mat','P9_E.mat','P10_E.mat','P11_E.mat','P12_E.mat','P13_E.mat','P14_E.mat','P15_E.mat','P16_E.mat','P17_E.mat','P18_E.mat','P19_E.mat','P20_E.mat','P21_E.mat','P22_E.mat','P23_E.mat','P26_E.mat','P27_E.mat','P28_E.mat','P29_E.mat','P30_E.mat','P31_E.mat','P32_E.mat','P33_E.mat','P34_E.mat','P35_E.mat','P36_E.mat','P37_E.mat','P38_E.mat','P39_E.mat','P40_E.mat','P41_E.mat','P42_E.mat','P43_E.mat','P44_E.mat','P45_E.mat','P46_E.mat','P47_E.mat','P48_E.mat','P49_E.mat','P50_E.mat','P51_E.mat','P52_E.mat','P53_E.mat');for i = 1:length(Files)datafiles=strtrim(Files(i,:));[param_E(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);endResults_LowCMP = param_E';%% High-CMP Conditioncondition = 2;Files=char('P3_I.mat','P4_I.mat','P7_I.mat','P8_I.mat','P9_I.mat','P10_I.mat','P11_I.mat','P12_I.mat','P13_I.mat','P14_I.mat','P15_I.mat','P16_I.mat','P17_I.mat','P18_I.mat','P19_I.mat','P20_I.mat','P21_I.mat','P22_I.mat','P23_I.mat','P26_I.mat','P27_I.mat','P28_I.mat','P29_I.mat','P30_I.mat','P31_I.mat','P32_I.mat','P33_I.mat','P34_I.mat','P35_I.mat','P36_I.mat','P37_I.mat','P38_I.mat','P39_I.mat','P40_I.mat','P41_I.mat','P42_I.mat','P43_I.mat','P44_I.mat','P45_I.mat','P46_I.mat','P47_I.mat','P48_I.mat','P49_I.mat','P50_I.mat','P51_I.mat','P52_I.mat','P53_I.mat');for i = 1:length(Files)datafiles=strtrim(Files(i,:));[param_I(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);endResults_HighCMP = param_I';%%
The file is in that location.
Please edit your posting to format the code.
Remove the existing code. Then click on the > button in the CODE section of the edit toolbar, which will open a code region in the editor. Copy your code out of your .m file and paste it into the code region here. That will insert the formatted code into your posting.
You might want to consider something like
filenums = [3:4, 7:23, 26:53];
low_names = "P" + filenums + "_E.mat";
high_names = "P" + filenums + "_I.mat";
for i = 1 : length(low_names)
datafiles = low_names(i);
param_E(:,i) = cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_LowCMP = param_E';
Sorry I do not fully understand.
Is this the correct code?
clear; clc; close;
%% SDA Analysis
% Use this file to calculate the SDA parameters for the High- and Low-CMP
% condition. Results will be stored in the 'Results' variables (either
% _HighCMP or _LowCMP).
% These files contain the following outcomes:
% Column 1 - Mediolateral critical time
% Column 2 - Mediolateral critical value
% Column 3 - Mediolateral short-term diffussion coefficient
% Column 4 - Mediolateral long-term diffussion coefficient
% Column 5 - Anterior-posterior critical time
% Column 6 - Anterior-posterior critical value
% Column 7 - Anterior-posterior short-term diffussion coefficient
% Column 8 - Anterior-posterior long-term diffussion coefficient
Files=char('6r1cop.csv');
for i = 1:length(Files)
take size() of variable
if any of the dimensions were 0, length is 0
otherwise length is max() of the dimensions
datafiles=strtrim(Files(i,:));
% Input
datapatho='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\';
% Output
datapathc='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\6r1cop.csv\';
%% Specify data path locations (of input and output) here:
% Input
datapatho='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\';
% Output
datapathc='C:\Users\megan\OneDrive\デスクトップ\914\データ\6\COP\6r1cop.csv\';
%% Low-CMP condition
condition = 1;
Files=char('6r1cop.csv');
for i = 1:length(Files)
datafiles=strtrim(Files(i,:));
[param_E(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_LowCMP = param_E';
%% High-CMP Condition
condition = 2;
Files=char('6r1cop.csv');
for i = 1:length(Files)
datafiles=strtrim(Files(i,:));
[param_I(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_HighCMP = param_I';
The second transmission is the original code. I would like to be able to use the code I sent the first time.
Files=char('6r1cop.csv');
sz = size(Files)
sz = 1×2
1 10
if any(sz == 0)
length_Files = 0;
else
length_Files = max(sz);
end
fprintf('according to definition, length of Files is %d\n', length_Files);
according to definition, length of Files is 10
fprintf('this compares to the result of length() which is %d\n', length(Files));
this compares to the result of length() which is 10
You can see that length() of '6r1cop.csv' is 10.
for i = 1 : length(Files)
try
Files(i,:);
fprintf('succeeded accessing row %d of Files\n', i);
catch ME
fprintf('failed accessing row %d of Files\n', i);
break
end
end
succeeded accessing row 1 of Files
failed accessing row 2 of Files
What does this tell you?
Answer: it tells you that your code
Files=char('6r1cop.csv');
for i = 1:length(Files)
datafiles=strtrim(Files(i,:));
is wrong. You want to loop over the rows in the Files variable, but length(Files) does not tell you how many rows are in Files. length(Files) tells you 0 if any dimension of Files is empty and otherwise it tells you the largest dimension of Files. When you have more columns then you have rows then length() will be the number of columns.
So you need to rewrite the for i = 1:length(Files) loop so that you are looping over the number of rows not over length() .
Thanks for the reply.
I have a CSV file with 3 columns and 3000 rows of data that I want to parse.
In that case, would it be ok to run the code you gave me?
%%
Files=char('6r1cop.csv');
%%
sz = size(Files)
if any(sz == 0)
length_Files = 0;
else
length_Files = max(sz);
end
%%
fprintf('according to definition, length of Files is %d\n', length_Files);
fprintf('this compares to the result of length() which is %d\n', length(Files));
%%
for i = 1 : length(Files)
try
Files(i,:);
fprintf('succeeded accessing row %d of Files\n', i);
catch ME
fprintf('failed accessing row %d of Files\n', i);
break
end
end
NO.
See the two lines I marked with "THIS WAS CHANGED"
%% SDA Analysis
% Use this file to calculate the SDA parameters for the High- and Low-CMP
% condition. Results will be stored in the 'Results' variables (either
% _HighCMP or _LowCMP).
% These files contain the following outcomes:
% Column 1 - Mediolateral critical time
% Column 2 - Mediolateral critical value
% Column 3 - Mediolateral short-term diffussion coefficient
% Column 4 - Mediolateral long-term diffussion coefficient
% Column 5 - Anterior-posterior critical time
% Column 6 - Anterior-posterior critical value
% Column 7 - Anterior-posterior short-term diffussion coefficient
% Column 8 - Anterior-posterior long-term diffussion coefficient
%% Specify data path locations (of input and output) here:
% Input
datapatho='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Input\';
% Output
datapathc='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Output\';
%% Low-CMP condition
condition = 1;
Files=char('P3_E.mat','P4_E.mat','P7_E.mat','P8_E.mat','P9_E.mat','P10_E.mat','P11_E.mat','P12_E.mat','P13_E.mat','P14_E.mat','P15_E.mat','P16_E.mat','P17_E.mat','P18_E.mat','P19_E.mat','P20_E.mat','P21_E.mat','P22_E.mat','P23_E.mat','P26_E.mat','P27_E.mat','P28_E.mat','P29_E.mat','P30_E.mat','P31_E.mat','P32_E.mat','P33_E.mat','P34_E.mat','P35_E.mat','P36_E.mat','P37_E.mat','P38_E.mat','P39_E.mat','P40_E.mat','P41_E.mat','P42_E.mat','P43_E.mat','P44_E.mat','P45_E.mat','P46_E.mat','P47_E.mat','P48_E.mat','P49_E.mat','P50_E.mat','P51_E.mat','P52_E.mat','P53_E.mat');
for i = 1:size(Files,1) %THIS WAS CHANGED
datafiles=strtrim(Files(i,:));
[param_E(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_LowCMP = param_E';
%% High-CMP Condition
condition = 2;
Files=char('P3_I.mat','P4_I.mat','P7_I.mat','P8_I.mat','P9_I.mat','P10_I.mat','P11_I.mat','P12_I.mat','P13_I.mat','P14_I.mat','P15_I.mat','P16_I.mat','P17_I.mat','P18_I.mat','P19_I.mat','P20_I.mat','P21_I.mat','P22_I.mat','P23_I.mat','P26_I.mat','P27_I.mat','P28_I.mat','P29_I.mat','P30_I.mat','P31_I.mat','P32_I.mat','P33_I.mat','P34_I.mat','P35_I.mat','P36_I.mat','P37_I.mat','P38_I.mat','P39_I.mat','P40_I.mat','P41_I.mat','P42_I.mat','P43_I.mat','P44_I.mat','P45_I.mat','P46_I.mat','P47_I.mat','P48_I.mat','P49_I.mat','P50_I.mat','P51_I.mat','P52_I.mat','P53_I.mat');
for i = 1:size(Files,1) %THIS WAS CHANGED
datafiles=strtrim(Files(i,:));
[param_I(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_HighCMP = param_I';
%%
"Thank you for your response. My goal this time is to use my own data. I tried adding some code, but it resulted in an error. Also, it says that the function cpdf_CMP is not defined. What should I do to set it up in this case?"
%%重心動揺計 解析
clear ; close
tic
%%
filename = '6still2COP.csv' ;%ファイル名をコピペしてください.「.csv」部分は触らないように.
ReadingStart = 1;%データ読み込み開始行
raw = csvread(filename,ReadingStart,0);%数値のみの行列の場合は関数このままでOK
% % x=raw(2:29000,1); %左右data列指定
% % y=raw(2:29000,2); %前後data列指定
%% 前処理
% srate=1000;%Sampling rate
% filtfreq=10;%Low pass filter の帯域
% order=4;%filterの次数
% time=30;%単位(s)
%
% [b, a]=butter(order,filtfreq/(srate/2),'low');%n次Butterworth filter 10Hz low pass
% ml=filtfilt(b,a,x);%COP_APorg
% ap=filtfilt(b,a,y);%COP_APorg
Text = raw;
%%
% function [rmsx,rmsy,MPFx,MPFy,SEn_x,SEn_y]=FP_analysis_RMS_MPF_SEN(Text)
%% Preparing data = filtering data and converting forces and moments into COP-coordinates
% FP_data = Text;
% trial_duration = 30; % 60-second trial duration 60秒間の立位保持
% Fs = 1000; %data was collected at 500 Hz サンプリング周波数 この論文では500㎐
%%
%参考論文 
%Kal EC, Young WR, Ellmers TJ.
%Balance capacity influences the effects of conscious movement processing on postural control in older adults.
%Hum Mov Sci. 2022 Apr;82:102933.
% doi: 10.1016/j.humov.2022.102933. Epub 2022 Feb 5. PMID: 35134657.
%https://pubmed.ncbi.nlm.nih.gov/35134657/
%https://osf.io/bwjmk/
%
%
%
%% Filtering the forces and moments using low-pass filter
% for i=1:3
% Fs_filter=Fs/2; F_cutoff = 10;% カットオフ周波数の半分 カットオフ周波数5㎐
% Fc=F_cutoff/.802; %correction for bidirectional filtering (Winter) 
% [B,A]=butter(4,Fc./Fs_filter, 'low');% バターワースフィルター2次
% FP_data_filt(:,i) = filtfilt(B, A, FP_data(:,i));
% end
%
% Fx=FP_data_filt(:,1);
% Fy=FP_data_filt(:,2);
% Fz=FP_data_filt(:,3);
% Estimating COP coordinates. Note that forces were measured in pounds.
% COPの座標の推定をしている。力はポンドで計測している。
% 今回は床反力計を使用しているため座標の推定をしている、我々は重心動揺計であるため必要ない
% 1はmediolateral 2 anteriorposterior
% COP_raw(1,:)=Fx;
% COP_raw(2,:)=Fy;%% SDA Analysis
% Use this file to calculate the SDA parameters for the High- and Low-CMP
% condition. Results will be stored in the 'Results' variables (either
% _HighCMP or _LowCMP).
% These files contain the following outcomes:
% Column 1 - Mediolateral critical time
% Column 2 - Mediolateral critical value
% Column 3 - Mediolateral short-term diffussion coefficient
% Column 4 - Mediolateral long-term diffussion coefficient
% Column 5 - Anterior-posterior critical time
% Column 6 - Anterior-posterior critical value
% Column 7 - Anterior-posterior short-term diffussion coefficient
% Column 8 - Anterior-posterior long-term diffussion coefficient
%% Specify data path locations (of input and output) here:
% Input
datapatho='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Input\';
% Output
datapathc='C:\Users\elmar\OneDrive\Desktop\Onderzoek\Brunel\Research\Projects\Brunel\CMP - IF vs EF and individual differences in older adults\SDA\Output\';
%% Low-CMP condition
condition = 1;
%Files=char('6still2COP');
Files=Text;
%%
for i = 1:size(Files,1) %THIS WAS CHANGED
datafiles=strtrim(Files(i,:));
[param_E(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_LowCMP = param_E';
%% High-CMP Condition
condition = 2;
Files=char('6still1COP');
for i = 1:size(Files,1) %THIS WAS CHANGED
datafiles=strtrim(Files(i,:));
[param_I(:,i)]=cpdf_CMP(datapatho,datapathc,datafiles,condition);
end
Results_HighCMP = param_I';
%%
here is the error.
エラー: strtrim
入力は文字列、文字配列または文字配列のセル配列でなければなりません。
エラー: Untitled (89)
datafiles=strtrim(Files(i,:));
Error message translates as approximately
Input must be a string, character array, or cell array of character arrays.
filename = '6still2COP.csv' ;%ファイル名をコピペしてください.「.csv」部分は触らないように.
ReadingStart = 1;%データ読み込み開始行
raw = csvread(filename,ReadingStart,0);%数値のみの行列の場合は関数このままでOK
There you are reading the contents of that particular file.
Text = raw;
csvread() never returns text. Never. Text always causes csvread() to error out, unless it is in a header row you told csvread to skip, or is in a leading column that you told csvread to skip.
So you raw variable will always be numeric, and that numeric value will be copied to the variable named Text
Later you have
Files=Text;
so Files will be numeric.
You then try to strtrim() a numeric value, which fails.
These days you should almost never use csvread(): use readtable() or readmatrix() or readcell() or fileread() or readlines() for most bulk reading. (There are some places to use textscan() as well.)

Sign in to comment.

Categories

Tags

Asked:

on 11 Mar 2023

Commented:

on 14 Mar 2023

Community Treasure Hunt

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

Start Hunting!