How can I output only part of a array?

조회 수: 2 (최근 30일)
Andy
Andy 2022년 6월 22일
답변: Voss 2022년 7월 3일
I wrote a code that plots a function for me. In the code, an array is given the name of the selected file. Example: FileName = 'Evaluation_114_1688.xlsm'
I would now like to save the figure as a jpg, which is why I used the command: saveas(gcf,FileName,'jpg')
But I would like to use only the numbers from the file name: 114_1688.jpg
How is it possible for me to select only the numbers from the file name?

답변 (2개)

KSSV
KSSV 2022년 6월 22일
FileName = ['114_1688','.jpg'] ;
saveas(gcf,FileName)
  댓글 수: 2
Andy
Andy 2022년 6월 22일
But everytime I run the code, I chose a other file. That means that the FileName is constantly changing. I need a command that recognizes the numbers in the array and names the jpg only after the numbers.
KSSV
KSSV 2022년 6월 22일
[filepath,name,ext] = fileparts('Evaluation_114_1688.xlsm') ;
FileName = strcat(name,'.jpg') ;
saveas(gcf,FileName)

댓글을 달려면 로그인하십시오.


Voss
Voss 2022년 7월 3일
This may work for you:
FileName = 'Evaluation_114_1688.xlsm';
new_name = regexp(FileName, '_([\d_]+\.)', 'tokens', 'once');
% ^ leading underscore
% ^^^^^^ followed by one or more (+) digit (\d == 0-9) or underscore (_) characters
% ^^ followed by a period
% ^ ^ group everything after the leading underscore, up to and including the period, in a "token" to be returned
new_name = [new_name{1} 'jpg']
new_name = '114_1688.jpg'

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by