Jump to content

CSV to HTML translation: Difference between revisions

Restore MATLAB example, deleted by Bbsingapore.
(Restore MATLAB example, deleted by Bbsingapore.)
Line 1,662:
<tr><td>Brians mother</td><td>I'm his mother;that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
=={{header|MATLAB}}==
{{incomplete|MATLAB|No escaping of '<' in '<angry' and '</angry'}}
The easiest way to import csv data into MATLAB is to save it in a csv file, then import the data using the "uiimport -file" command. The result of which will be a cell array with each entry being a string delimited by the newline character.
 
Example:
<lang MATLAB>>> data
 
data =
 
'Character,Speech'
'The multitude,The messiah! Show us the messiah!'
[1x109 char]
'The multitude,Who are you?'
'Brians mother,I'm his mother; that's who!'
'The multitude,Behold his mother! Behold his mother!'</lang>
 
The other way is to copy and paste the data, but you will have to manually add all of the newline characters and string quotations your self. This is messy and infeasible for large amounts of data, but it is still a valid input.
 
Example:
<lang MATLAB>>> csvData = ['Character,Speech' sprintf('\n')...
'The multitude,The messiah! Show us the messiah!' sprintf('\n')...
'Brians mother,<angry>Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!</angry>' sprintf('\n')...
'The multitude,Who are you?' sprintf('\n')...
'Brians mother,I''m his mother; that''s who!' sprintf('\n')...
'The multitude,Behold his mother! Behold his mother!']
 
csvData =
 
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!</lang>
 
So, to be able to accept both type of inputs, the function "csvToHTHML()" tests to see if the input is a string as in example 2. If the input is a string it converts the data to a cell array of strings formatted in the exact same way MATLAB formats the first example. The output of the function will be a properly formatted HTML table, which includes the "<THEAD>" around the first row of data so that special formatting can be applied to the column titles.
 
<lang MATLAB>function htmlOutput = csvToHTML(csvData)
 
if ischar(csvData)
newlineIndex = find( (csvData == char(10)) ); %find all newline characters
text = cell( numel(newlineIndex),1 ); %preallocate space for processed data
for i = (numel(newlineIndex):-1:1) %iterate backwards
text{i} = csvData(newlineIndex(i)+1:end);
csvData(newlineIndex(i):end) = []; %delete the newline and everything after it
end
csvData = text;
clear text;
end
htmlOutput = '<table>';
for i = (1:numel(csvData))
if i == 1 %If first entry, then include <thead>
csvData{i} = [char(10) char(9) '<thead><tr>' char(10) char(9) char(9) '<td>'...
csvData{i} '</td>' char(10) char(9) '</tr></thead>' char(10)];
else %Otherwise, the data is a regular table row
csvData{i} = [char(9) '<tr>' char(10) char(9) char(9) '<td>' csvData{i}...
'</td>' char(10) char(9) '</tr>' char(10)];
end %if
%Convert each comma to its HTML equivalent
for j = fliplr(find( (csvData{i} == ',') ))
csvData{i} = [csvData{i}(1:j-1) '</td>' char(10) char(9) char(9) '<td>' csvData{i}(j+1:end)];
end %for
%We could make this faster by preallocating htmlOutput but that is
%more work than necessary to provide a basic solution
htmlOutput = [htmlOutput csvData{i}];
end %for
 
htmlOutput = [htmlOutput '</table>'];
end %csvToHTML</lang>
 
Ouput:
<lang html5>>> csvToHTML(data)
 
ans =
 
<table>
<thead><tr>
<td>Character</td>
<td>Speech</td>
</tr></thead>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.