print matlab output string to xlsx spreadsheet

3 Ansichten (letzte 30 Tage)
Bheki Ngobe
Bheki Ngobe am 23 Jan. 2016
Kommentiert: Walter Roberson am 23 Jan. 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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jan. 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 Kommentare
Bheki Ngobe
Bheki Ngobe am 23 Jan. 2016
Bearbeitet: Walter Roberson am 23 Jan. 2016
Hi, just one more question,
Is there a way I can specify which cell should store my license plate ?
Because I am actually reading a license plate number, then generate a random number.
I would like to store each license plate with the random number generated, I want to pair them each time an application is run. And I don't want it to overwrite existing data on excel. Below was my modification on your code, which worked but it prints random number in the next row whereas I want it in the next column (same row with license number).
% results.Text
license_plate = results.Text
drvLicNum = randi([1000000 10000000],1,1) % Generating random number
license_plate = license_plate(~isspace(license_plate)) %eliminate spaces in betwwen characters
%export to excel
fid = fopen('C:\Users\Computer_Vision_in_Quality_Control\Project\data.csv', 'wt');
fprintf(fid, '%s\n', license_plate);
fprintf(fid,'%.0f\n',drvLicNum); %random number
fclose(fid);
Walter Roberson
Walter Roberson am 23 Jan. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by