1

I need to load data from file whose file names are enh0.dat, enh1.dat, enh2.dat, ..., up to 128 times. So I need to store the file name in a string variable and load data from a file, whose name is stored in the string variable, then store the loaded data in a specified variable for plotting later. But the MATLAB load command loads the data into a variable with a specific name (name of the file containing the data) which does not allow me to automate the use of plot command.

Is it possible to store the name of the file from which I need to load data, in a string variable? I also checked out this MATLAB newbie: problem reading in file when the file name is stored in a string, but it doesn't look like it works for me. I get this error when trying to plot the result of textscan:

??? Error using ==> plot Conversion to double from cell is not possible.

This is the code used:

indx = [1:128];
enh_file_cntr = 0;
enh_pre = 'enh';
gain_pre = 'gain';
[enh_file_cntr_str, errmsg1] = sprintf('%d', enh_file_cntr);
enh_file_name = strcat('enh', enh_file_cntr_str, '.dat');
[gain_file_cntr_str, errmsg1] = sprintf('%d', enh_file_cntr);
gain_file_name = strcat('enh', gain_file_cntr_str, '.dat');
fid_enh = fopen(enh_file_name, 'r');
fid_gain = fopen(gain_file_name, 'r');
enh_data = textscan(fid_enh, '%f', 128);
gain_data = textscan(fid_gain, '%f', 128);
subplot(2,3,1);
plot(indx, enh_data, 'b', indx, gain_data, 'r');

If this works I will be incrementing the value of enh_file_cntr in a for loop. How can I fix the above code?

2 Answers 2

1

First: The plot function accepts inputs that are of the numerical matrix type. You are trying to input the output of the textscan function which is a cell array. In your case, the first element of the cell array contains a numerical matrix, but the object is not a numerical matrix itself. To retrieve the numerical matrix stored inside the cell, use:

enh_data = enh_data{1};

prior to calling the plot function. Note: When accessing the elements of a cell array, always use curly braces, ie {}. When accessing the elements of a numerical matrix, use regular parentheses, ie (). If you have a cell array that contains multiple cells, you can slice it up using ().

Of course, a cell in a cell array can itself contain a cell array, but maybe don't worry about this for now :-)

Second: Make sure to close any file you open with fopen. ie, once you've used textscan, then close the file again with fclose(fid_enh).

Third, I'm not quite sure I understand what you mean by storing the filename in a string? You appear to be doing exactly that in the code snippet above with the enh_file_name variable?

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I am doing the same thing here, but as you pointed out the result of the textscan function is of type cell array, which cannot be used by plot I put the above code to show as an example of what I wanted to do I will try out your suggestion So if I understand correctly, the result of textscan is like an excel spreadsheet, where the box number (0, 0) contains all of the 128 data points from the file in a 1x128 array? Is that how I should interpret this? Thanks for your response :)
by the way, how do I insert line breaks when I comment here?
@user13267: there are no line breaks in comments. Also, if you found the answer helpful, please consider accepting it.
@user13267 I've added a bit more explanation to my answer, but your interpretation of textscan for your current problem is correct. However, if the text you were scanning in had two columns, then the result would be a 1x2 cell array. The first element would contain the first column of data, and the second element would contain the second column. Experiment a bit and you should get the idea. Also, if I answered your question, then please tick the check mark next to my answer (as Jonas has suggested).
1

Sounds like you can also use the load command to read your data.

All you need to do to get around the problem with the variable name created is to specify and output variable with the load command.

I think you initially tried

load(enh_file_name)

and found the variable created was filename in the variable enh_file_name.

You just need to use

enh_data = load(enh_file_name)

The side benefit of this is that you wont have to deal with cell variables.

1 Comment

Thank you very much I think this can be an alternate way of doing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.