Looping through Files with For - Error or Bug?

Hi!
I have several files I need to edit, and I was trying to write a script to do that for me. I need to run several matlab scripts to get different variables in the workplace, and then run a particular matlab function on those variables.
So this is the code I tried to use:
for n=1:9
filename = ['N' int2str(n) '.dat';
bears = ['/Users/DMSenter/Dropbox/REU/Project6/mainparameters' int2str(n) '.m'];
run(bears)
run('/Users/DMSenter/Dropbox/REU/Project6/IGLE.m'(kernel,parameters,flags))
end
The different script files are named N1.m,N2.m, N3.m,...,N9.m. The output is supposed to be saved in an equivalent dat file (this is why I reset the filename each time). My problem though is as follows: the line
bears = ['/Users/DMSenter/Dropbox/REU/Project6/mainparameters' int2str(n) '.m'];
Always returns an error:
Error: File: shortcut.m Line: 17 Column: 11 The expression to the left of the equals sign is not a valid target for an assignment.
When I run just this line in the console/interpreter however, it works as I'd like it to work. I tried several different variable names (bears is just the last one I tried). I always get this error. Why?
In regards to calling the script-file, I know the run command works. Will it work the way I wrote for calling a function? Something like
function [Xpath,Xend,acf_x,t]=IGLE(kernel,parameters,flags)
Thanks for the help!
- Mike

Answers (1)

Instead of
filename = ['N' int2str(n) '.dat';
try to include the trailing bracket:
filename = ['N' int2str(n) '.dat'];

1 Comment

Or better yet, use sprintf:
filename = sprintf('N%d.dat', n);

This question is closed.

Asked:

on 2 Dec 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!