create output structure within a function

Hi,
I have problems getting the data output in a structure. I made a function in which I do several things. In the end, I want to have an output structure which contains different parts of the data.
this is how the script starts: function [output] = interpolate_csv (filename)
In the end I create the output like this: output = struct('pathstr','name','ext','baseline', 'postbaseline','postbaseline_baseline_corrected_0_500ms', 'postbaseline_baseline_corrected_500_1000ms', 'postbaseline_baseline_corrected_1000_1500ms', 'postbaseline_baseline_corrected_1500_2000ms', 'postbaseline_baseline_corrected_2000_2500ms', 'postbaseline_baseline_corrected_2500_3000ms', 'postbaseline_baseline_corrected_3000_3500ms', 'postbaseline_baseline_corrected_3500_4000ms')
All I get is this:
output =
''
What do I need to add to the script?
Thank you for your help.
best, Mariska

 Accepted Answer

unless you mean this:
output = struct('pathstr','name','ext','baseline', 'postbaseline',{'postbaseline_baseline_corrected_0_500ms', 'postbaseline_baseline_corrected_500_1000ms', 'postbaseline_baseline_corrected_1000_1500ms', 'postbaseline_baseline_corrected_1500_2000ms', 'postbaseline_baseline_corrected_2000_2500ms', 'postbaseline_baseline_corrected_2500_3000ms', 'postbaseline_baseline_corrected_3000_3500ms', 'postbaseline_baseline_corrected_3500_4000ms'})
output =
1x8 struct array with fields:
pathstr
ext
postbaseline
>> output(1)
ans =
pathstr: 'name'
ext: 'baseline'
postbaseline: 'postbaseline_baseline_corrected_0_500ms'

5 Comments

Hi,
thanks for your answers and sorry for not making myself clear. I don't have much programming experience. Anyway, I did not forget to insert the data...I just don't know how to get it into the structure! :-)
I get all the outputvalues when I do it as below. But since I have more than one file, I need to make a loop. But first I want to learn how to make a neat data structure, preferably with the name of the csv-file and than in it all the things I now listed in the first line.
thank you for your help.
Mariska
function [pathstr, name, ext, baseline, postbaseline,postbaseline_baseline_corrected_0_500ms,postbaseline_baseline_corrected_500_1000ms,postbaseline_baseline_corrected_1000_1500ms,postbaseline_baseline_corrected_1500_2000ms,postbaseline_baseline_corrected_2000_2500ms,postbaseline_baseline_corrected_2500_3000ms,postbaseline_baseline_corrected_3000_3500ms,postbaseline_baseline_corrected_3500_4000ms] = interpolate_csv5 (filename)
% read csv file
data = csvread('test2.csv', 5); % read the file, skip first five lines
% filename as output
[pathstr, name, ext] = fileparts(filename)
% extreme values become missing (= NaN)
data_temp = data(:, [8 11]);
data_temp(data_temp >= 9) = NaN;
data_temp(data_temp < 1) = NaN;
data(:, [8 11]) = data_temp;
time = (1:length(data))'% time = data(:,1);
data_temp8 = data(:, 8);
data_temp11 = data(:, 11);
% interpolate NaNs with a certain max
%data_interpolated8 = interp1(time(isfinite(data_temp8)), data_temp8(isfinite(data_temp8)), time);
%data_interpolated11 = interp1(time(isfinite(data_temp11)), data_temp11(isfinite(data_temp11)), time);
data_interpolated8 = interp_nan(data_temp8, 4);
data_interpolated11 = interp_nan(data_temp8, 4);
% average colomn 8 and 11
averaged_filtered_data = (data_interpolated8 + data_interpolated11)/2
% postbaseline
postbaseline = averaged_filtered_data(data(:, 2) > 0)
% baseline
baseline = averaged_filtered_data(data(:,2) == 0);
baseline = baseline(end-4:end);
baseline = mean (baseline)
% postbaseline minus baseline
postbaseline_baseline_corrected = postbaseline - baseline
postbaseline_baseline_corrected_0_500ms = mean(postbaseline_baseline_corrected(1:30,1))
postbaseline_baseline_corrected_500_1000ms = mean(postbaseline_baseline_corrected(31:60,1))
postbaseline_baseline_corrected_1000_1500ms = mean(postbaseline_baseline_corrected(61:90,1))
postbaseline_baseline_corrected_1500_2000ms = mean(postbaseline_baseline_corrected(91:120,1))
postbaseline_baseline_corrected_2000_2500ms = mean(postbaseline_baseline_corrected(121:150,1))
postbaseline_baseline_corrected_2500_3000ms = mean(postbaseline_baseline_corrected(151:180,1))
postbaseline_baseline_corrected_3000_3500ms = mean(postbaseline_baseline_corrected(181:210,1))
postbaseline_baseline_corrected_3500_4000ms = mean(postbaseline_baseline_corrected(211:240,1))
end
Okay, let's try a simple one first. In your previous function [output] = interpolate_csv(filename), can you add this line at the end of the function? See what is the output. You'll get an idea and then you can add more stuff.
output=struct('pathstr',pathstr,'name',name);
Ah, this works! But not for the other ones (for example postbaseline) that are a column of numbers. The output contains only one cell. The output structure should be multidimensional.
output=struct('postbaseline',[postbaseline_baseline_corrected_0_500ms,postbaseline_baseline_corrected_500_1000ms]);
or
output=struct('postbaseline',[postbaseline_baseline_corrected_0_500ms;postbaseline_baseline_corrected_500_1000ms])
Thank you for the replies. I have it working now and also created a loop so that it saves data from multiple csv files.
data=struct('filename',filename,'baseline',baseline,'postbaseline',[postbaseline_baseline_corrected_0_500ms,postbaseline_baseline_corrected_500_1000ms...etc 3500_4000]);
I want to have an excel or csv file as output. This matrix should have the size of 10 columns (1 for filename, 1 for the baseline value, 8 different post-baseline values). Each unique .csv file should start on a new line.
xlswrite('filename',baseline,postbaseline);????

Sign in to comment.

More Answers (1)

Do you mean
output=struct('pathstr',name,'ext',baseline,...)?
check the syntax of struct() again.

3 Comments

Yes, I used this function. The layout of the text above was a bit shifted so I modified it now. But how can I make it work?
If I run your line directly, I got
??? Error using ==> struct
Field and value input arguments must come in pairs.
Check to make sure you always have the struct('param',value) with 'param' and value paired.
@Mariska: You did not get Jiangs point. It looks like you define the STRUCT with only the field names, but forget to insert the data also. But the syntax of STRUCT is: struct(name1, data1, name2, data2, ...).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!