could you create a script that will open a text file from my usb in matlab

You can use the uigetfile function to open a text file from your USB drive. Here is an example script:

main.m
[filename, filepath] = uigetfile('*.txt', 'Select Text File from USB');
if isequal(filename,0) || isequal(filepath,0)
    disp('User selected Cancel');
else
    fullpath = fullfile(filepath, filename);
    fileID = fopen(fullpath);
    % do something with the file
    fclose(fileID);
end
289 chars
10 lines

Explanation:

  • uigetfile pops up a dialog box that allows you to choose a file to open. The *.txt argument filters the selectable files to only show text files.
  • If the user clicks "cancel" in the dialog box or closes it without selecting a file, uigetfile returns 0. The script checks for this and displays a message.
  • If the user selects a file, uigetfile returns the file name and full file path, which are stored in the filename and filepath variables, respectively.
  • The fullfile function combines the filename and filepath into a single string that represents the full path to the selected file.
  • The fopen function opens the file in read mode and returns a file identifier (fileID) that is used for subsequent file operations.
  • At this point, you can read the contents of the file with functions such as fgetl or fgets, or you can use text import functions such as readtable or textscan to read the file. Once done reading the file, you should close the file with the fclose function.

Make sure to replace the '*.txt' argument with the appropriate file extension if your file is not a text file. Also, make sure to adjust the file reading code to match the format of your file.

related categories

gistlibby LogSnag