Illegal use of reserved keyword "for".

3 views (last 30 days)
Matt b
Matt b on 6 Jun 2018
Edited: Stephen23 on 6 Jun 2018
I have the following code which replaces text when I directly put a string in the "nextLine" statement, however when I try to input elements of a cell array with a delimiter the function breaks at the "for" statement. Can anybody please explain how I am using this incorrectly, or direct me to a better way to format this input?
Thank you for your time.
function replaceLine(inputPath,outputPath)
inputFile = fopen(inputPath,'r');
outputFile = fopen(outputPath,'w');
while ~feof(inputFile)
nextLine = fgets(inputFile);
if any(contains(strsplit(nextLine,'3 3'),'3 ')) %first repsonses
newLine = '' for n = 1:length(cell{1,1})
newLine = [newLine '' cell{1,1}(n)]
end
end
fprintf(outputFile,nextLine);
end
fclose(inputFile);
fclose(outputFile);
end

Answers (1)

Stephen23
Stephen23 on 6 Jun 2018
Edited: Stephen23 on 6 Jun 2018
Instead of this
newLine = '' for n = 1:length(cell{1,1})
You should do this:
newLine = '';
for n = 1:length(cell{1,1})
(adding a semicolon or comma would be sufficient, but putting the code on two lines is much clearer)
  4 Comments
Matt b
Matt b on 6 Jun 2018
Thank you for your time. I have fixed this by declaring the variables as global because they were not accessible by the function workspace. I have a lot to learn about becoming a better programmer, but this accomplishes the task at hand for now.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!