hi I have multiple .csv files and I want to randomly choice 1 to run how do I right the code to only choice on of them?

1 回表示 (過去 30 日間)
i am writing a code for the game battle ships and i need to be able to import multiple .csvfiles for the computer to run from but each game the computer needs to choice a file to use how do i make it randomly choice which file to use?

採用された回答

Image Analyst
Image Analyst 2021 年 12 月 30 日
Try this:
folder = pwd; % Wherever the csv files are
filePattern = fullfile(folder, '*.csv');
% Get a directory listing.
fileList = dir(filePattern)
numFiles = length(fileList)
% Get a random index
randomIndex = randi(numFiles)
% Get the filename.
fullFileName = fullfile(fileList(randomIndex).folder, fileList(randomIndex).name)
  1 件のコメント
James Webber
James Webber 2021 年 12 月 30 日
thanks for the help but it is having issues with the file name
i named my computer Choices
and the files are called
computer choice 1
computer choice 2
computer choice 3
compouter choice 4
and my program is a .m

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWord games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by