How to save a structure as .mat?

조회 수: 3 (최근 30일)
Ibro Tutic
Ibro Tutic 2015년 11월 4일
댓글: Rena Berman 2021년 5월 6일
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
Rena Berman
Rena Berman 2021년 5월 6일

(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

채택된 답변

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.
Ibro Tutic
Ibro Tutic 2015년 11월 4일
I figured it out, thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (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