print matlab output string to xlsx spreadsheet
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Bheki Ngobe
il 23 Gen 2016
Commentato: Walter Roberson
il 23 Gen 2016
Hi everyone please help.
I have read number of responses from here, and some youtube videos but my problem seem to be a bit different.
I am trying to print license_plate string (cl12ml23) to excell spread sheet. the license prints each character in its own cell and I want it printed to one cell. Sometimes the code comes with this error when I am trying book examples " Error using <a href="matlab:matlab.internal.language...' identifier: 'MATLAB:xlswrite:dlmwrite' " below is my code portion
% results.Text
license_plate = results.Text
drvLicNum = randi([1000000 10000000],1,1) % Generating number for drivers license number
drvStore = drvLicNum;
string = license_plate + drvLicNum;
fprintf('%s', string)
%Export to Excel option
%load license_plate.mat
license_plate = license_plate(~isspace(license_plate)) %eliminate spaces in betwwen characters
%filename = 'C:\Users\Makhosi\\Project\data.xlsx';
ca = {license_plate};
%A = [string]; %working
%xlswrite(filename,ca);
xlswrite('C:\Users\Computer_Vision_in_Quality_Control\Project\data.xlsx',ca);
I would appreciate if I can be shown where to go to get help with this. you can also contact me on 820400516@student.uj.ac.za.
I thank you in advance
0 Commenti
Risposta accettata
Walter Roberson
il 23 Gen 2016
csvwrite() cannot write character strings.
dlmwrite() can only write character strings if you are tricky about it by using '' as the field specifier and using '%c' or '%s' in the optional Precision parameter -- and it can only print one string per line when you do that (and all of the lines need to be the same length.) dlwrite() is only intended for pure numeric values and anything else is a hack.
xlswrite() has two possible modes.
If you are not using MS Windows, or if you are using MS Windows but Excel cannot be found or cannot be started, then xlswrite() operates in 'basic' mode. 'basic' mode can only write strings if the cell array contains entirely strings with no numeric values, and if in every column the strings are exactly the same length; in that case 'basic' mode will create a .csv file with one character per entry, comma separated, no distinction made between adjacent cells. This is pretty restrictive and seldom to be desired.
If you are using MS Windows, and Excel can be found and started, then xlswrite() can write mixed arrays without problem.
If you are each character in its own cell, chances are high that Excel could not be found or started and that 'basic' mode has been used. There is nothing you can do about that while still using xlswrite(), except to fix your Excel.
To write strings to files without using xlswrite() or dlmwrite() or csvwrite(), then you need to fopen() a file for writing, fprintf() or fwrite() contents to it, and fclose() the file afterwards. Writing .xls files this way is not easy, as xls files use a proprietary binary format. Writing .xlsx files is easier, as those use a more open text format, but it still isn't much fun. Writing plain .txt or .csv files is relatively easy, though:
fid = fopen('C:\Users\Computer_Vision_in_Quality_Control\Project\data.csv', 'wt');
fprintf(fid, '%s\n', license_plate);
fclose(fid);
3 Commenti
Walter Roberson
il 23 Gen 2016
fid = fopen('C:\Users\Computer_Vision_in_Quality_Control\Project\data.csv', 'at'); %changed
fprintf(fid, '%s,%.0f\n', license_plate, drvLicNum);
fclose(fid);
Notice the change from 'wt' to 'at' . That is crucial to avoid writing over the existing content.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!