필터 지우기
필터 지우기

How can I check if a table contains numbers only?

조회 수: 6 (최근 30일)
Adham Elkhouly
Adham Elkhouly 2020년 5월 9일
답변: Image Analyst 2020년 5월 9일
Good evening,
I am trying to develop a MATLAB application that adds values to a table by reading them from an excel file, but these values need to be all numbers and the x-values in the table cannot be repeated twice. Can someone please help me?
Thank you
Edit: I forgot to mention that the program works, reads the data from the sheet, writes them on the table with the readtable functiion but I cannot implement validtation.
  댓글 수: 2
Mario Malic
Mario Malic 2020년 5월 9일
I would suggest trying to import data into a cell (or other type that will make it easier to write it to the table) variable and remove the values you don't need from there. Then write it to table.
Walter Roberson
Walter Roberson 2020년 5월 9일
Can the x values be repeated once? 1 2 3 3 4 5 5 6 6 7 8 for example?

댓글을 달려면 로그인하십시오.

채택된 답변

Peng Li
Peng Li 2020년 5월 9일
I guess your question re the x-values isn't quite clear.
For the first part of your question, it is tricky to determine whether an input is a number or contains other stuffs as well. There isn't an elegent way I think. You could either try (1) to convert it to a double, if it gives you a valid double, that orignal input contains only number (or number related stuffs +-E etc), or (2) to examine whether all members of an input belong to 0123456789-+E or whatever elements you think might be used in a valid number expression (using ismember). Either way, try to import the orignal Excel sheet using '%q' option for each column to force the table to be a cell of char, and using cellfun to handle each element of the cell.
Below an example:
testTbl =
4×2 table
A B
_______ _______
{'123'} {'123'}
{'4E5'} {'d' }
{'-56'} {'67' }
{'888'} {'999'}
>> isNum = cellfun(@(x) all(ismember(x, '0123456789-+E')), testTbl.A);
if isNum
testTbl.A = str2double(testTbl.A);
end
>> testTbl
testTbl =
4×2 table
A B
_____ _______
123 {'123'}
4e+05 {'d' }
-56 {'67' }
888 {'999'}
>> isNum = cellfun(@(x) all(ismember(x, '0123456789-+E')), testTbl.B);
if isNum
testTbl.B = str2double(testTbl.B);
end
>> testTbl
testTbl =
4×2 table
A B
_____ _______
123 {'123'}
4e+05 {'d' }
-56 {'67' }
888 {'999'}

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 5월 9일
Did you try the readmatrix() function? I believe it will import only numbers. Otherwise use xlsread() to get only the numbers.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by