Is there a way to assign the 'end' value to a variable?

13 次查看(过去 30 天)
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

采纳的回答

Steve Eddins
Steve Eddins 2012-7-10
No, you can't assign the indexing value of "end" to a variable. You have to use size, or length, or numel in order to assign the value to a variable.
  1 个评论
Jan
Jan 2012-7-10
编辑:Jan 2012-7-10
It is surprising that 'end' does not work, while ':' is accepted as string. Anyhow, size() is better. +1
So Sergej needs: b = length(list_of_files(i).name).

请先登录,再进行评论。

更多回答(4 个)

F.
F. 2012-7-10
And did you try strread ??
[A, B, ...] = strread('str', 'format')
[ NameStart, NameEnd ] = strread( list_of_files(i).name, '%s_%s')

Sergi
Sergi 2012-7-10
Thanks F.
The thing is i've already solved my problem getting parts from a string.
Thanks anyway for the strread() tip, but i'm still out of curiosity if it is possible to assign 'end' to a variable.
I'm sure all the examples using 'end' I can think of can be equaly programmed with other options, but maybe one day it'll be need it, so
is there a way to use 'end value' from a variable?
Cheers
Sergi
  3 个评论
Sergi
Sergi 2012-7-10
Thanks to all. Sorry about the posting stuff. My first time!! My bad.
Sergi
Sergi 2012-7-10
I'm aware of a=b being better than a=b(1:end). Sorry, it was just a not very good example the one i wrote. My real routine is needed to extract just a part from the whole string, never all of it...
thanks anyway

请先登录,再进行评论。


Jan
Jan 2012-7-10
编辑:Jan 2012-7-10
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
Daniel Shub 2012-7-10
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.

Community Treasure Hunt

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

Start Hunting!

Translated by