Unable to save data or continue through loop
4 次查看(过去 30 天)
显示 更早的评论
Hey guys,
It seems the code works below, with all my variable ending up as the correct size, at least until it gets to the save function. I'm trying to get each file that's being saved to have the suffix 'i', which should be a counter of sorts. I'm intending each file to end in 1, 2, ......100, etc.
However, the save feature doesn't work, and the FOR loop doesn't go through another iteraion. I'm pretty sure I'm missing brackets or paranthesis somewhere.
Here's the error;
Brace indexing is not supported for variables of this type.
Error in NMDIII_Process (line 119)
save(strcat(Filenamesave{1},'_',signal), 't', 'tm', 'v','xRaw', 'x', 'yRaw','y','n','FileNames');
Here's the code.
clc
clear
%%This sections is to load the correct folder to gather data from
% ***Below is the same code for Linux!
%rootfolder = '/L:/Data/';
% ***Don't forget to swith the slashes!
rootfolder = 'T:\Data\';
% Chopped = '/chopped';
Chopped = '\Chopped';
prompt = 'Enter date of experiment to be analyzed: ';
DataDate = input(prompt, 's');
Directory = strcat(rootfolder,DataDate,Chopped);
%% This is to select the save file name
prompt = 'Enter the name for the processed files according to mmddyyyy_bug_media_oC ';
Filenamesave = input(prompt, 's');
%% This is to select the signal type. This should have already been done with datachop =]
prompt = 'Enter the Signal Type DEF, LFM or SUM ';
signal = input(prompt, 's');
%% Below are some code alternatives to that were previously tried. Maybe I should delete these......
% FileNames=dir(Directory);
%FileNames=dir(fullfile(Directory, '*.txt'))
% FileNames=dir(Directory);
% FileNames(1:2) = [];
% FileNames = {FileNames.name};
%%
FileNames=dir(Directory);
FileNames = {FileNames.name};
FileNames = FileNames(contains(FileNames, '.txt'));
%Below removes '.' and '..' from the array, only keeping files with the
%.txt suffix. Your 'K' should decrease by two.
%FileNames = FileNames(contains(string({FileNames.name}), '.txt'));
disp(' ');
disp(' ');
disp('File structure generated');
disp(' ');
disp(' ');
disp('Data Processing Starting');
disp(' ');
disp(' ');
%% This is the run inputs
% signal = 'DEF';
% epflplot = 'N';
% rawplot = 'N';
% fftplot = 'N';
% ft = 20000;
% bt = 30;
%%
for FilesToRead = 1 : length(FileNames)
%This one doesn't work yet =[ Saving for a possible later date
%y=fopen(FileNames(FilesToRead).name );
%filename = sprintf(' <--maybe this????)
% y = load(FileNames{FilesToRead});
y = load(fullfile(Directory, FileNames{FilesToRead}));
y = {y};
n = 400000;
for i=1:length(y)
x{i}=(1:length(y{i}))';
end
for ii=1:length(y)
for k=1:length(y{ii})/n
coeffs{ii}(k,:)=polyfit(x{ii}(n*(k-1)+1 : k*n),y{ii}(n*(k-1)+1 : k*n),1);
yfit{ii}(k,:)=coeffs{ii}(k,2)+coeffs{ii}(k,1)*x{ii}(n*(k-1)+1 : k*n);
end
end
for ii=1:length(y)
dd{ii}= subsref(yfit{ii}.', substruct('()', {':'})).';
dd2{ii}=y{ii}(1:length(dd{ii}))-dd{ii}';
end
for i=1:length(dd2)
t{i}=zeros(1,length(dd2{i}));
end
t{1}=1:length(dd2{1});
for i=2:length(dd2)
t{i}= t{i-1}(end)+[1:length(dd2{i})];
end
clear coeffs dd i ii k prompt ws yfit
yRaw=y; xRaw=x;
x=t; y=dd2;
clear dd2 t
% Window for variance
n = 100000;
for i=1:length(y)
for k=1:length(y{i})/n
a(k)=var(y{i}(n*(k-1)+1:k*n));
end
v{i}=a;
a=[];
end
t{1}=1:length(v{1});
for i=2:length(y)
t{i}=t{i-1}(end)+[1:length(v{i})];
end
for i=1:length(t)
tm{i} = t{i}.*((n/20000)/60); % time in minutes
end
%Save variable output.
%v = variance
%%Worrying about the save data later
% 'i' in the save feature is to name each file in order, ie 1,2, ....100)
save(strcat(Filenamesave{1},'_',signal,'_', 'i'), 't', 'tm', 'v','xRaw', 'x', 'yRaw','y','n','FileNames');
disp('Thats all folks!')
end
% Variables that were not saved with this update. Might be needed when
% making graphs
% -v7.3
% Names
%PathNames
%ft
Thank you!
0 个评论
采纳的回答
Star Strider
2019-9-14
I can’t run your code. However, since you are not defining the character array ‘Filenamesave’ as a cell array earlier in your code (at least not that I can see), don’t refer to it that way in your save call.
Try this instead:
save(strcat(Filenamesave,'_',signal,'_', 'i'), 't', 'tm', 'v','xRaw', 'x', 'yRaw','y','n','FileNames');
Also 'i' is going to write the character ‘i’ to your file name. If you want it to write the current value of ‘i’, use this instead:
num2str(i)
Of course, using sprintf is much easier:
savefilename = sprintf('%s_%s_%02d.mat', Filenamesave, signal, i);
save(savefilename, 't', 'tm', 'v','xRaw', 'x', 'yRaw','y','n','FileNames');
Experiment to get the result you want.
Since I can’t test this, I am presenting it as UNTESTED CODE.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!