i am asking the user for a file and i wana transform that file into a matrix, i dont know how many lines or colums there is i only know that is a square matrix and that the numbers are seperated with space bar
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
vicente simas
am 27 Nov. 2018
Bearbeitet: Image Analyst
am 28 Nov. 2018
This is my code right now. My code only saves the last line of the file and saves it in an array aline. (I cannot use load or dlmread().
nme = input('nome do ficheiro >','s');
fid = fopen(nme,'r');
if fid == -1
disp('ficheiro nao encontrado');
else
while feof(fid)==0
aline=fgetl(fid);
end
closeresult=fclose(fid);
if closeresult==0
disp('ficheiro fechado com sucesso')
else
disp('erro')
end
end
0 Kommentare
Akzeptierte Antwort
Jeremy Hughes
am 27 Nov. 2018
T = readtable(filename,'Delimiter',' ');
A = T.Variables
3 Kommentare
Jeremy Hughes
am 28 Nov. 2018
Maybe I missed part of this conversation. I didn't see a reference to his restrictions. There are restrictions when working on embedded systems.
If there are some limitations, I don't know if my solution will work in that context.
Image Analyst
am 28 Nov. 2018
Bearbeitet: Image Analyst
am 28 Nov. 2018
In his post at the top, it says
"(I cannot use load or dlmread()."
Usually such things are done by professors who want students to do things manually rather than use handy built-in functions because they want them to do it the long way, to learn it better I guess.
Weitere Antworten (1)
Image Analyst
am 27 Nov. 2018
Try this:
% 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 ~exist(startingFolder, 'dir')
% 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)
fid = fopen(fullFileName,'rt');
lineCounter = 1;
if fid == -1
disp('ficheiro nao encontrado');
else
while feof(fid)==0
thisLine = fgetl(fid);
if ischar(thisLine)
allLines{lineCounter} = thisLine;
end
lineCounter = lineCounter + 1;
end
closeresult=fclose(fid);
if closeresult==0
disp('ficheiro fechado com sucesso')
else
disp('erro')
end
end
celldisp(allLines);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!