Updating variables as they go through a loop

  • Context: MATLAB 
  • Thread starter Thread starter Larry Gopnik
  • Start date Start date
  • Tags Tags
    Loop Variables
Click For Summary

Discussion Overview

The discussion revolves around a problem in MATLAB programming related to loading multiple .txt files into an array without overwriting previous filenames. The focus is on coding techniques and troubleshooting errors encountered in the process.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant seeks help on how to store multiple filenames in a loop without overwriting them.
  • Another participant suggests concatenating the filenames into an array.
  • A later reply indicates that the participant is encountering errors when trying to store filenames in an array, specifically mentioning an issue with cell contents assignment.
  • One participant advises removing parentheses when assigning values to the array and notes potential issues with slashes in the path.
  • Another participant explains that the error arises from initializing the array as a 2-D array of doubles instead of a cell array, suggesting a correction to the initialization method.

Areas of Agreement / Disagreement

Participants generally agree on the need to use a cell array for storing filenames, but there are varying suggestions on how to handle specific coding issues and errors.

Contextual Notes

Limitations include the initial misunderstanding of array types in MATLAB and the specific error messages encountered, which may depend on the context of the code being executed.

Larry Gopnik
Gold Member
Messages
34
Reaction score
19
Hi all,

This might be a simple question but I cannot seem to find any solution online

I'm currently building a MatLab code for my project which involves in the ageing of certain artefacts and paintings, I've got most of it built but can't solve the simplest of problems of the start of the code

It requires loading a certain number of .txt files (the number of which the user specifies) into my code. So far I have for this particular part of the code:

j=input('Please input the number of data files');
lw = [400,450,500,550,600,650,700,750,800,880];

for i = 1:j

[filename,path]= uigetfile('*.txt','open .txt file');


end

How do I get it so that there are multiple filenames saved and so that the previous filename isn't overwritten by the new filename?

Basically, the code should make filename1, filename2, filename3 and so on from the loop instead of just one filename which is constantly overwritten...

Thank you

Larry
 
Physics news on Phys.org
concatenate the file names into an array...
 
  • Like
Likes   Reactions: jim mcnamara and Larry Gopnik
Dr Transport said:
concatenate the file names into an array...

My goodness of course, It's so simple but I missed it! Thank you! What an idiot I am

Quick question, I'm trying to store the filenames into the array and get errors

n = 10; %Number of slices
j=input('Please input the number of data files ');
lw = [400,450,500,550,600,650,700,750,800,880]; %slices in nm
z = zeros(n,j);
pathA = zeros(j);

for i = 1:j

[filename,path]= uigetfile('*.txt','open .txt file');
pathA{i} = (filename);
end

disp(z);
disp(pathA);

I get the error:

"Cell contents assignment to a non-cell array
object.

Error in Build_1 (line 14)
pathA{i} = (filename);
"

I'm very thankful for the help - up to now I've only used Matlab for pure maths, so I'm finding the more "practical" sides of the code quite difficult and new

Larry
 
Try leaving out the parenthesis in line 14... also, if there is a \ in the path, that causes problems also
 
Larry, the reason why you're getting the error is that you initially created pathA as a 2-D array of doubles with this line:

pathA = zeros(j);

Then you triedto index it with braces, which means that you expect it to be a cell array. But it's NOT, hence the error. You should initialize pathA like this:

pathA = cell(j, 1);

Then use it like this:

pathA{i} = filename;

No parentheses needed. The FAQ will explain it all: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

You might also like this FAQ on how to process a sequence of files: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Finally having slashes, forward or backward, in your filename string will NOT be a problem.
 
  • Like
Likes   Reactions: Larry Gopnik

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K