How to save a structure as .mat?

3 visualizzazioni (ultimi 30 giorni)
Ibro Tutic
Ibro Tutic il 4 Nov 2015
Commentato: Rena Berman il 6 Mag 2021
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 Commenti
Stephen23
Stephen23 il 26 Dic 2020
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 il 6 Mag 2021

(Answers Dev) Restored edit

Accedi per commentare.

Risposta accettata

Kirby Fears
Kirby Fears il 4 Nov 2015
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 Commenti
Ibro Tutic
Ibro Tutic il 4 Nov 2015
Modificato: Ibro Tutic il 4 Nov 2015
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 il 4 Nov 2015
I figured it out, thanks.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by