How to save a structure as .mat?

1 次查看(过去 30 天)
I am trying to save a 1x6 structure named 'PIN' as a .mat file. When I attempt to use save(newfilename.mat,'PIN'), I get this error: Attempt to reference field of non-structure array.
How would I go about saving this structure as a .mat file with the specified name?
projectdir = 'C:\Users\it58528\Documents\Dig Test'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newfolder, subfolderi);
mkdir(newsubfolder);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename, ext] = fileparts(thisfile);
data = csvread(thisfile,5,2);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:17);
%FIX THIS PIN(fileidx).serialnumber = data(1,2); //serial number
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newfilename = fullfile(newsubfolder, filenamei);
save(newfilename.mat,'PIN'); %%%%FIX
end %files within subfolder
end %subfolders within folder
end %folders
  2 个评论
Stephen23
Stephen23 2020-12-26
Original question by Ibro Tutic on 4th November 2015 retrieved from Google Cache:
How to save a structure as .mat?
I am trying to save a 1x6 structure named 'PIN' as a .mat file. When I attempt to use save(newfilename.mat,'PIN'), I get this error: Attempt to reference field of non-structure array.
How would I go about saving this structure as a .mat file with the specified name?
projectdir = 'C:\Users\it58528\Documents\Dig Test'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newfolder, subfolderi);
mkdir(newsubfolder);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename, ext] = fileparts(thisfile);
data = csvread(thisfile,5,2);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:17);
%FIX THIS PIN(fileidx).serialnumber = data(1,2); //serial number
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newfilename = fullfile(newsubfolder, filenamei);
save(newfilename.mat,'PIN'); %%%%FIX
end %files within subfolder
end %subfolders within folder
end %folders

请先登录,再进行评论。

采纳的回答

Kirby Fears
Kirby Fears 2015-11-4
newfilename is a variable in your workspace when you define it as
newfilename = fullfile(newsubfolder, filenamei);
In your first argument to save(), you say "newfilename.mat", which attempts to access the mat field of object newfilename. Hence the error Attempt to reference field of non-structure array.
Try this syntax instead:
save(newfilename,'PIN');
Or if you really want to tag ".mat" on the end explicitly:
save([newfilename,'.mat'],'PIN');
  2 个评论
Ibro Tutic
Ibro Tutic 2015-11-4
编辑:Ibro Tutic 2015-11-4
I tried that initially and it doesn't save the structure 'PIN'. It just takes the original .csv file that I am reading and saves it to a new directory. What I need it to do is save the PIN structure as a .mat file with the same name as the original file.
I think the issue lies in the filename somewhere, I feel like my program might be considering .csv a part of the file name and when it saves, it is automatically considered a .csv file? To add on to this, this looks like the issue. newfilename is saved as 'theoriginalfilename'.csv. What I need to do is just take 'theoriginalfilename' and leave off the .csv, any ideas on how to go about this?
When I tried tag .mat on at the end explicitly, the file was saved as 'the original file name'.csv.mat.
When I use the command window and type save('PIN','PIN'), it does what I want it to do, but it saves the file in the location of the matlab program. It's fairly useless to me since I have to do this to thousands of files and typing save('PIN','PIN') every time would be idiotic.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by