I am working on small project, part of it is quit away from my field, and I don't know if I can do it with matlab.
this part is related to dealing with txt files
it is simply reading line by line from the attached file q1.txt and printing the 1st line on screen, then writing the response of the user will be either a number or group of letters such as 100 , yes, no, up , down
and based on the response the program is supposed to show certain infromatino from the secnod txt file a1.
is there any way to achive that using matlab?

 採用された回答

Image Analyst
Image Analyst 2020 年 5 月 30 日
編集済み: Image Analyst 2020 年 5 月 30 日

0 投票

Try this:
promptMessage = sprintf('Select your text file on the next dialog box.');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if contains(buttonText, 'Cancel', 'IgnoreCase', true)
return;
end
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.txt');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
userPrompt = sprintf('%s (type only Enter to quit now): ', strtrim(textLine));
userResponse = input(userPrompt, 's');
% Bail out if they just typed Enter.
if isempty(userResponse)
break;
end
% Print out user's response.
fprintf('You entered %s.\n', userResponse);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);

3 件のコメント

Abdulrahman Odhah
Abdulrahman Odhah 2020 年 5 月 30 日
編集済み: Abdulrahman Odhah 2020 年 5 月 30 日
That is a perfect code, Thank you very much, I didn't even know this technique in matlab.
I am just wondering if I can accumulate the user responses in a column vector to be called later on or to be used in looping in the next steps of the program?
Also, one more thing, how can I make the script above as a function to be called whenver needed ?
Thanks in advance.
Image Analyst
Image Analyst 2020 年 5 月 30 日
Index the variable with the loop counter
userResponse{lineCounter} = input(userPrompt, 's');
Abdulrahman Odhah
Abdulrahman Odhah 2020 年 5 月 30 日
Perfect, thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by