Effacer les filtres
Effacer les filtres

Import data 800*7 textfile into cell array using textscan

1 vue (au cours des 30 derniers jours)
Arthur Hajaali
Arthur Hajaali le 14 Août 2016
Modifié(e) : Jeremy Hughes le 5 Juil 2017
Hi everyone, I am trying to read a textfile with 800 rows and 7 columns I have manage to import it into an array but the issue is that the array in the form of (1,7) and when i click on a cell the 800 values appears but i would for everything to be display in one big array. Here is the code I am using:
%%%Open file and import data into a multiple cellular array
filename = 'shipsspecs.txt';
fid = fopen(filename,'r');
%%%Check if the file exist and display error otherwise
if fid == -1
fprintf('Could not access this file', filename);
error('This file does not seem to exist... Please check for any typo error of your filename')
end
%%Initialise variable requires to import textfile into cell array
formatSpec = '%s%f%f%f%s%s%s'; %Defines the format of the variable of each column
%headerlines =1; %Defines the number of line that need to be consider as header to import them all as string regardeless of the formatspecs
delimiter = ',';
%%Importatoion function
ships_array = textscan(fid, formatSpec, 'HeaderLines', 1, 'Delimiter', delimiter,'ReturnOnError', false);
fclose(fid);
And my table is presented this way (picture 1)
Rather than the way (picture 2)
Could someone help please :)

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 14 Août 2016
Modifié(e) : Azzi Abdelmalek le 14 Août 2016
You can change your code
filename = 'shipsspecs.txt';
fid = fopen(filename,'r');
if fid == -1
fprintf('Could not access this file', filename);
error('This file does not seem to exist... Please check for any typo error of your filename')
end
formatSpec = '%s';
headerlines =1;
delimiter = ',';
ships_array = textscan(fid, formatSpec, 'HeaderLines', 1, 'Delimiter', delimiter,'ReturnOnError', false);
fclose(fid);
v=reshape([s{:}],7,[])'

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 14 Août 2016
v=[];
for k=1:numel(ships_array)
if iscell(ships_array{k});
v=[v ships_array{k}];
else
v=[v num2cell(ships_array{k})];
end
end
v
  2 commentaires
Arthur Hajaali
Arthur Hajaali le 14 Août 2016
Excellent, Cheers! working perfectly! Can you explain me what was wrong in my code though because that is how matlab advise me to do it with their tutorial...
Azzi Abdelmalek
Azzi Abdelmalek le 14 Août 2016
Modifié(e) : Azzi Abdelmalek le 14 Août 2016
There is nothing wrong with your code. The data are not displayed like you wanted.

Connectez-vous pour commenter.


Jeremy Hughes
Jeremy Hughes le 5 Juil 2017
Modifié(e) : Jeremy Hughes le 5 Juil 2017
You might also want to consider READTABLE. Each "column" of the file will be imported as a table variable in one array with a convenient display. It will also import the numbers as double, and the text as text--as in your initial code snippit. It will just all be wrapped in a table.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by