0

I have data stored in the .tdms format, gathering the data of many sensors, measured every second, every day. A new tdms file is created every day, and stored in a folder per month. Using the convertTDMS function, I have converted these tdms files to mat files.

As there are some errors in some of the measurements(e.g. negative values which can not physically occur), I have performed some corrections by loading one mat file at a time, do the calculations and then save the data into the original .mat file.

However, when I try to do what I described above in a loop (so: load .mat in folder, do calculations on one mat file (or channel therein), save mat file, repeat until all files in the folder have been done), I end up running into trouble with the limitations of the save function: so far I save all variables (or am unable to save) in the workspace when using the code below.

for k = 1:nFiles
w{k,1} = load(wMAT{k,1});
len = length(w{k,1}.(x).(y).(z));

pos = find(w{k,1}.(x).(y).(z)(1,len).(y)<0); %Wind speed must be >0 m/s
    for n = 1:length(pos)
    w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)) = mean([w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)+1),...
        w{k,1}.(x).(y).(z)(1,len).(y)(pos(n)-1)],2);
    end
save( name{k,1});
%save(wMAT{k,1},w{k,1}.(x),w{k,1}.ConvertVer,w{k,1}.ChanNames);
end

A bit of background information: the file names are stored in a cell array wMAT of length nFiles in the folder. Each cell in the cell array wMAT stores the fullfile path to the mat files. The data of the files is loaded and saved into the cell array w, also of length nFiles. Each cell in "w" has all the data stored from the tdms to mat conversion, in the format described in the convertTDMS description. This means: to get at the actual data, I need to go from the

  • cell in the cell array w{k,1} (my addition)
  • to the struct array "ConvertedData" (Structure of all of the data objects - part of convertTDMS)
  • to the struct array below called "Data" (convertTDMS)
  • to the struct array below called "MeasuredData" (convertTDMS) -> at this level, I can access the channels which store the data.
  • to finally access/manipulate the values stored, I have to select a channel, e.g. (1,len), and then go via the struct array to the actual values (="Data"). (convertTDMS) In Matlab format, this looks like "w{1, 1}.ConvertedData.Data.MeasuredData(1, len).Data(1:end)" or "w{1, 1}.ConvertedData.Data.MeasuredData(1, len).Data".

To make typing easier, I took

x = 'ConvertedData';
y = 'Data';
z = 'MeasuredData';

allowing me to write instead:

w{k,1}.(x).(y).(z)(1,len).(y)

using the dot notation.

My goal/question: I want to load the values stored in a .mat file from the original .tdms files in a loop to a cell array (or if I can do better than a cell array: please tell me), do the necessary calculations, and then save each 'corrected' .mat file using the original name.

So far, I have gotten a multitude of errors from trying a variety of solutions, going from "getfieldnames", trying to pass the name of the (dynamically changing) variable(s), etc.

Similar questions which have helped me get in the right direction include Saving matlab files with a name having a variable input, Dynamically Assign Variables in Matlab and http://www.mathworks.com/matlabcentral/answers/4042-load-files-containing-part-of-a-string-in-the-file-name-and-the-load-that-file , yet the result is that I am still no closer than doing manual labour in this case.

Any help would be appreciated.

Community
  • 1
  • 1
Monte Cristo
  • 131
  • 1
  • 12

1 Answers1

0

If I understand your ultimate goal correctly, I think you're pretty much there. I think you're trying to process your .mat files and that the loading of all of the files into a cell array is not a requirement, but just part of your solution? Assuming this is the case, you could just load the data from one file, process it, save it and then repeat. This way you only ever have one file loaded at a time and shouldn't hit any limits.

Edit

You could certainly make a function out of your code and then call that in a loop, passing in the file name to modify. Personally I'd probably do that as I think it's neater solution. If you don't want to do that though, you could just replace w{k,1} with w then each time you load a file w would be overwritten. If you wanted to explicitly clear variables you can use the clear command with a space separated list of variables e.g. clear w len pos, but I don't think that this is necessary.

sclarke81
  • 1,739
  • 1
  • 18
  • 23
  • Thank you, yes. My problem is that when I run the load-convert-save m-file for 1 file, I can then not use this solution in a for loop. In the stand-alone m-file, I can clear all the temporary workspace variables so I end up with the (now modified) contents of the mat file being saved. However, this is not so easily done in the loop, as I don't know if I can re-initialize *everything* in the loop. Would making a function out of my stand-alone m-file help (so I can loop around it)? – Monte Cristo Aug 20 '14 at 07:43