How to create a table in MATLAB?

2

I'm doing a job that I want to create the following table:

I have tried several commands like table and readtable and it always returns me the following message:

Undefined function 'readtable' for input arguments of type 'char'. 
 Undefined function 'table' for input arguments of type 'char'.

I also tried to import from excel and it did not work. I know it's something simple, but I'm not getting it.

    
asked by anonymous 16.07.2015 / 18:10

1 answer

2

If you already have the table in Excel, the way it might be easier is to export the Excel data to a file CSV (Comma-Separated Value) . Just go to the " File " menu and choose the " Save as " option. In the dialog window, choose the file type as CSV:

MyExcelisinPortuguese,soitusesthesemicolon(;)insteadofthecomma(,)astheseparator,sincethecommainPortugueseisusedtoseparatedecimalsinnumbers(thedotisusedtoseparatedecimalsinEnglish,sothereisnoconfusioninreadingthefile).TheCSVfile,forme,wasleftwiththiscontent:

Componente;AmostraMT(phr);AmostraLN(phr)Elastrômetro;100;100Cargas;110;60Plastificante;34;9Ativadordevulcanização;4,5;4,5Auxiliardefluxoeprocesso;4,5;4,5Agenteaceleradordevuncanização;5,5;6

Onceyouhavethisfile,youcanimportthedataintoanarrayinMATLABusingthefollowingcode:

arquivo=fopen('c:\temp\teste.csv','r');dados=textscan(arquivo,'%s%s%s','delimiter',';')fclose(arquivo);

Whatthiscodedoesis(1)openthefiletoread,(2)readthedatainthespecifiedformatsanddelimiter,and(3)closethefile.NotethatIforcedthedelimiterasasemicolon,becauseofthefileexportedbymyExcel.YoumaynotneedtodothesameifyourExcelisinEnglishandexportbyseparatingthedatawithacomma.NotealsothatI'mformattingeverythingasastring(using%sforallcolumns).Ideally,youshouldimportthenumericvaluesbyconvertingthemappropriatelytofloat,butyoucandothislater(oreditthefiletomanuallyremovetheheaderlineifthatdataisnotimportant).

Theresultisanarraywiththefollowingcolumnsandrows:

>>dadosdados={7x1cell}{7x1cell}{7x1cell}

Andyoucanaccessthevaluesusingthefollowingsyntaxtogetthecolumnsindividually:

>>dados{1}ans='Componente''Elastrômetro''Cargas''Plastificante''Ativadordevulcanização''Auxiliardefluxoeprocesso''Agenteaceleradordevuncanização'>>dados{2}ans='AmostraMT(phr)''100''110''34''4,5''4,5''5,5'>>dados{3}ans='AmostraLN(phr)''100''60''9''4,5''4,5''6'
  

Q.:Thereisalsothefunctioncalled csvread , which also exists in   2013 version (although the documentation does not make it very clear). She can   help you, but is less flexible in reading the files and only reads data   (ie your file can not have text). Another alternative is the xslread function, already described in this another question .

ISSUE:

Just to make it more complete, I put here a version that does the conversion of numerical data appropriately. Remember that I need to replace the comma with a semicolon because my data was exported via an Excel in Portuguese. The conversion to number only from the second line (note the% index of% used to access the columns) is due to ignore the header and keep it as a string.

  

The (2:end) function that I use is very interesting because it allows   use / call another function (in my example, the cellfun function) to   each value in an array.

Here is the code:

arquivo = fopen('c:\temp\teste.csv', 'r');
dados = textscan(arquivo,'%s%s%s','delimiter',';')
fclose(arquivo);

% Conversões:
% Para cada coluna (2 e 3), primeiro substitui a vírgula (,) por ponto (.)
% e então converte para número apenas os dados da segunda linha em diante
% (note o (2:end) na indexação dos dados).
dados{2}(2:end) = strrep(dados{2}(2:end), ',', '.')
dados{2}(2:end) = cellfun(@str2num, dados{2}(2:end), 'UniformOutput', 0)

dados{3}(2:end) = strrep(dados{3}(2:end), ',', '.')
dados{3}(2:end) = cellfun(@str2num, dados{3}(2:end), 'UniformOutput', 0)

Which now results in values converted to numbers within MATLAB:

>> dados{2}

ans = 

    'Amostra MT(phr)'
    [   100]
    [   110]
    [    34]
    [4.5000]
    [4.5000]
    [5.5000]
    
16.07.2015 / 20:44