Is there a way to assign the 'end' value to a variable?
Show older comments
Hello everyone,
this is my first question. Sorry if it's too simple or easy, but i couldn't find the answer at Google.
I was programming a simple script to locate all file names in a folder. Most of them had a similar name, with '_' character in it (ex: file_name.mat), but not all of them. I wanted only the firt part of the name, from those with '_' character.
I did something like this (is a shorter version of my code):
for i=1:length(list_of_files)
b=findstr('_',list_of_files(i).name)-1;
if isempty(b)
b='end' %That's my question
end
another_list{i}=list_of_files(i).name(1:b)
end
I know that i could just use 'length(list_of_files(i).name)' instead of 'end', but i was out of curiosity to ask about if it was possible to assign 'end' to a variable, and how?
I've already tried something like:
b=end;
b='end';
Doesn't work!
Thanks
Sergi
Accepted Answer
More Answers (4)
F.
on 10 Jul 2012
And did you try strread ??
[A, B, ...] = strread('str', 'format')
[ NameStart, NameEnd ] = strread( list_of_files(i).name, '%s_%s')
Sergi
on 10 Jul 2012
0 votes
3 Comments
Please, Sergej, do not post important information to define the problem as answer. Editing the original question is much more efficient for adding clarifications.
Please note that the method you use for copying strings is not efficient. A=B(1:end) takes remarkably more time then A=B, while the later creates even a shared data copy and saves memory in addition. Therefore the answers do not hit the point of your question. We hesitate to fix an inefficient method, when the problem does not occur in efficient solutions.
Sergi
on 10 Jul 2012
Sergi
on 10 Jul 2012
for i=1:length(list_of_files)
b = strfind(list_of_files(i).name, '_') - 1; % FINDSTR is deprecated
if isempty(b)
another_list{i} = list_of_files{i}; % Curly braces!
else.name(1:b)
another_list{i} = list_of_files{i}(1:b(1));
end
end
"b(1)" considers even multiple underscores. But if an underscore appears, the file extension is cut off also, while it is kept for names without underscores.
strtok can do the conversion also. You can even avoid the loop:
another_list = strtok(list_of_files, '_');
But the file extension problem is still not addressed.
Daniel Shub
on 10 Jul 2012
While I have not verified it, I think if you really wanted to assign
b='end'
and then use it for indexing, that you could overload subsref. I cannot think of a good reason you would want to do this.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!