0

I would like to loop the following script so that the 00001 will sequentially increase (to 00002, 00003 etc.) in the order 1 to 100. The 00001 appears 3 times: Under %% Initialize variables: r5004b_00001.dat and under %% Allocate imported array to column variable names: Angle00001 and Intensity00001

%% Initialize variables.
filename = sprintf('E:\XRD\Enamel\r5004b_00001.dat');
startRow = 5;

%% Format string for each line of text:
formatSpec = '%14f%f%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Allocate imported array to column variable names
Angle00001 = dataArray{:, 1};
Intensity00001 = dataArray{:, 2};

%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans;

1 Answer 1

2

For the file name, this is a good idea, and here is the solution (I use concatenation for clarity only, sprintf is of course enough):

number = 3;
s = sprintf('%05d', number); % will produce '00003'
filename = ['E:\XRD\Enamel\r5004b_'  s '.dat'];

For the variables, do not do this. It is better to use arrays or, if you really like having many names, a structure with dynamic field names:

strct.(['angle' s]) = ...

If you really want to achieve what you ask for, it can be done with

eval(['a' s ' = 1 + 1;'])
Sign up to request clarification or add additional context in comments.

4 Comments

How do you transfer the files in the structure to the workplace? I need to perform curve fitting and the data can not be toggled.
@Mosawi, I am not sure I understand you. What do you mean by "files in the structure" and "data cannot be toggled"?
The variables are confined within a structure array, I would like to transfer them to the main workplace. I am using the curve fitting app which can only select data directly from the workplace and not those in a structure array.
Then maybe you indeed need the last solution—with eval. I am not using apps yet, preferring to script everything for reuse and reproducibility.

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.