From User Input to File Name

조회 수: 30 (최근 30일)
MATLAB_Soldier
MATLAB_Soldier 2022년 10월 17일
댓글: MATLAB_Soldier 2022년 10월 20일
Hi all,
I am a bit stuck with my script. What I am trying to achieve is to request an input from the user (a number). Then make this input part of the file name that I am trying to export.
This is the code I have written so far, but unfortunately, it does not work.
Where am i going wrong?
clc
clear all
close all
A=2000
prompt = {'Project Number:'};
dlgtitle = 'Input';
dims = [1 50];
definput = {' '};
answer = inputdlg(prompt,dlgtitle,dims,definput)
writematrix(A,'num2str(answer)+TEST.csv')
Many thanks
  댓글 수: 1
KSSV
KSSV 2022년 10월 17일
writematrix(A,'num2str(answer)+TEST.csv')
What you are expecting to do from the above line?

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

채택된 답변

Steven Lord
Steven Lord 2022년 10월 18일
With the clarification in your comment on the answer from @Enrico Gambini, I would use string operations along with the fullfile function.
n = 4000;
filename = n + "_TEST.csv"
filename = "4000_TEST.csv"
pathname = fullfile("c:\temp", filename)
pathname = "c:\temp/4000_TEST.csv"
Note that because the code execution functionality for MATLAB Answers runs on Linux, fullfile used / as the file separator. If you ran that on Windows it would use \. See the filesep function.
computer
ans = 'GLNXA64'
And of course you could combine the two lines together to avoid creating the temporary variable filename, if you don't need that separately.
pathname2 = fullfile("c:\temp", n + "_TEST.csv")
pathname2 = "c:\temp/4000_TEST.csv"
  댓글 수: 1
MATLAB_Soldier
MATLAB_Soldier 2022년 10월 20일
Thank you for your answer. I have used the code above.
Have a good day!

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

추가 답변 (1개)

Enrico Gambini
Enrico Gambini 2022년 10월 17일
Try to use the function "strcat" and "string"
str=string(answer);
writematrix(A,strcat(str,'+TEST.csv'))
  댓글 수: 1
MATLAB_Soldier
MATLAB_Soldier 2022년 10월 18일
Thank you Enrico. This one seems to work. However, I have realised that it is not going to work when I want to define the path, such as:
prompt = {'Project Number:'};
dlgtitle = 'Input';
dims = [1 50];
definput = {' '};
answer = inputdlg(prompt,dlgtitle,dims,definput)
str=string(answer);
writematrix(A,'C:\Users\XYZ\Documents\MATLAB\TEST.csv')
I have tried this variant to include user input in the file name, but as I expected, it didn't work.
writematrix(A,'C:\Users\XYZ\Documents\MATLAB\'strcat(str,'TEST.csv'))
What is the issue?
Many thanks!

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

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by