I choose 0 files to construct matrix, all matrix entries are 0

1 view (last 30 days)
Márcis Pinups
Márcis Pinups on 3 Nov 2021
Answered: Navya Singam on 10 Nov 2021
I have a code in which person can choose the files from which he can construct matrix with signals, and vector vith a time signal.
I need an additional code for the case if I don't choose the files: respectively files=0, then code should go trough the matrix and vector possitions and plug 0 evrywhere.
clear vector_t_sig simulink_matrix
m=input('how many folders/files you want to use? - ');
files=m;
signal_max=5;
simulink_matrix=cell(files,signal_max);
for i=1:files
[file,path] = uigetfile('MultiSelect','on');
if length(file)>signal_max
file_temp{1}=file;
file=file_temp;
end
for j=1:length(file)
load([path,file{j}])
for k=1:signal_max
if exist('y_inv_L')
position=1;
sig_temp=y_inv_L;
clear y_inv_L;
%and so on for the other possitios%
simulink_matrix{i,position}=sig_temp;
vector_t_sig{i,1}=t_sig;

Answers (1)

Navya Singam
Navya Singam on 10 Nov 2021
Hi,
You may use the if and else block and "zeros" function to add the code for when the number of selected files is 0. zeros function creates an array and it can be converted into cell array using the num2cell function. I have added the additional code required for reference.
clear vector_t_sig simulink_matrix
m=input('how many folders/files you want to use? - ');
files=m;
signal_max=5;
simulink_matrix=cell(files,signal_max);
%% if else block
if files==0 %if number of files is 0, use the zeros function to generate required matrix of all 0's
simulink_matrix = num2cell(zeros(m,n)) %m,n denote the number of rows and column.
vector_t_sig = num2cell(zeros(m,n)) % num2cell function is used for converting numerical array to cell array
else
for i=1:files
[file,path] = uigetfile('MultiSelect','on');
if length(file)>signal_max
file_temp{1}=file;
file=file_temp;
end
for j=1:length(file)
load([path,file{j}])
for k=1:signal_max
if exist('y_inv_L')
position=1;
sig_temp=y_inv_L;
clear y_inv_L;
%and so on for the other possitios%
simulink_matrix{i,position}=sig_temp;
vector_t_sig{i,1}=t_sig;

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by