MATLAB Answers

Renaming a struct in v2020b

8 ビュー (過去 30 日間)
Hi,
I am currently writting code to detect peaks in a signal.
The data is pre-recorded in a series of dynamically named .mat files (i.e patient_1, patient_2 etc.) I want to try and move away from dynamic naming.
I want the user to select a .mat file say 'patient_1' for example and rename it to 'patient_X'. This should make it easier to write generic functions and apply them.
I have tried a similar method to inputing as one would with csv and other files with uigetfile:
[fileName, pathName] = uigetfile('*.mat'); % only looks for .mat files
pat_data = readstruct(fullfile(pathName,fileName)) ; % concat file path and type
However the new readstuct (2020b) function does not allow .mat data, only .XML so this does not work.
I have also tried just renaming the stuct like a standard variable:
pat_XXX = load(fileName) ; % Rename the struct
however I then get a 1x1 struct called pat_XXX with the orignal stuct inside it.
I really think this should be an easy task but I cannot figure out where I am going wrong and a look for answers has only provided how to change struct feildnames
Kind Regards,
Christopher

採用された回答

Image Analyst
Image Analyst 2020 年 11 月 15 日
I dont' think that renaming the files all to the same name is a good idea. Why can't you just deal with the original names. Have a listbox and load it up with all the .mat files. Let the user click on the one he wants to process and click the "Go" or "Analyze" button to process that particular file(s). In your function that processes a single file, just read in the mat file and get the fieldnames, something like (untested)
function results = AnalyzeSingleFile(fullFileName);
results = []; % Initialize
storedStructure = load(fullFileName);
% Now we need to extract the correct & relevant variable from the stored structure.
% OPTION 1: If field names vary (which was a bad idea)..
% Get the first structure and put it into pat_XXX
f = fieldnames(storedStructure);
pat_XXX = storedStructure.(f{1});
% OPTION 2: If field names are all the same, like each mat file has a field called Data, (much better), then...
pat_XXX = storedStructure.Data;
% Now process pat_XXX to get results...
  1 件のコメント
Christopher McCausland
Christopher McCausland 2020 年 11 月 16 日
Hi @image Analyst,
I just wanted to say thank you. With your suggestions I was able to get what I needed. The data thankfully does have the same feildnames. This initial code is more for testing algorithums and generation of preformance data. Therefore it was benifical to easily load in data without having to use the manual import data.
It would be really nice to see the readstuct command work with .mat data in the furture so import methods are standerdised, though I am guessing there are limitations which is why its not in the 2020b release.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 11 月 15 日
pat_data = load(fullfile(pathName, fileName));
save(newFileName, 'pat_data', '-struct')
Though you could also consider
movefile( fullfile(pathName, fileName), newFileName );
  1 件のコメント
Christopher McCausland
Christopher McCausland 2020 年 11 月 16 日
Hi @Walter Roberson,
Thankyou for the suggestion, I had considered save but not with the '-struct' option which would have worked. I will also have a look at movefile. Still learning the ticks of this programming trade!
Christopher

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by