From: Harry on
Hi,

Here's my question:
I'm working with a ".txt"-file which I load into matlab. I also need a part of the filename (number) to be saved into a variable. How should I do this?

For example, I have a file "car_137.txt":
- how should I save 137 as a number (in lets say for example an unknown "a")

Thanks in advance! Greetings from Brussels!
From: Wayne King on
Harry <zekollari(a)hotmail.com> wrote in message <1197312723.494940.1270287750664.JavaMail.root(a)gallium.mathforum.org>...
> Hi,
>
> Here's my question:
> I'm working with a ".txt"-file which I load into matlab. I also need a part of the filename (number) to be saved into a variable. How should I do this?
>
> For example, I have a file "car_137.txt":
> - how should I save 137 as a number (in lets say for example an unknown "a")
>
> Thanks in advance! Greetings from Brussels!

Hi Harry, I'm sure there are many ways. Does the number always follow an underscore and precede a "."? If so, you can do this

filename = 'car_137.txt';
% get the underscore position
Us = find(double(filename)==95);
% get the period
Pd = find(double(filename)==46);
Num = str2num(filename(Us+1:Pd-1));

Wayne
From: Wayne King on
Harry <zekollari(a)hotmail.com> wrote in message <1197312723.494940.1270287750664.JavaMail.root(a)gallium.mathforum.org>...
> Hi,
>
> Here's my question:
> I'm working with a ".txt"-file which I load into matlab. I also need a part of the filename (number) to be saved into a variable. How should I do this?
>
> For example, I have a file "car_137.txt":
> - how should I save 137 as a number (in lets say for example an unknown "a")
>
> Thanks in advance! Greetings from Brussels!

Another way using regexp()

filename = 'car_137.txt';
Us = regexp(filename,'_');
Pd = regexp(filename,'\.');
Num=str2num(filename(Us+1:Pd-1));

Wayne
From: Bruno Luong on
My preferable way:

num = str2double(regexp(filename,'(?<=(\_))\d*(?=\.)','match'))

% Bruno
From: Harry on
Thanks for helping me out! You guys are great. Greetings from Brussels!