WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Matlab Code auf Wordpress veröffentlichen

 

quantfinance
Forum-Anfänger

Forum-Anfänger


Beiträge: 21
Anmeldedatum: 23.05.10
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 25.08.2012, 23:11     Titel: Matlab Code auf Wordpress veröffentlichen
  Antworten mit Zitat      
Hallo,

ich möchte gern meinen Matlab-Code auf einem Wordpress-Blog veröffentlichen. Ich habe dazu auf folgender Seite einen Code gefunden:

https://github.com/jkitchin/matlab-wordpress

Leider verstehe ich nicht sehr viel davon, wenn es um eine Java-Verknüpfung geht. Insbesondere gibt mir mein Matlab bei der folgenden Zeile eine Fehlermeldung:

Code:

import redstone.xmlrpc.XmlRpcClient;
client = XmlRpcClient(server,0);
 


Die Fehlermeldung ist die folgende:

Zitat:

Error: File: blogpost.m Line: 230 Column: 8
Arguments to IMPORT must either end with ".*"
or else specify a fully qualified class name: "redstone.xmlrpc.XmlRpcClient"
fails this test.


Kann mir jemand dabei helfen, warum diese Zeilen nicht durchlaufen und wozu sie benötigt werden? Ich bedanke mich schon einmal im Voraus. Ich poste im Folgenden noch den kompletten Code des m-files:

Code:

function post_id = blogpost(mfile, dryrun, verbose)
% post the html output of publishing an m-file to matlab.cheme.cmu.edu
% postid = blogpost('my_mfile.m')
%
% if dryrun is true, no post is run, and the local post-processed html i
% opened in a browser.
%
% if verbose is true, there is more output (not supported yet).
%
% you need to create a function called blogCredentials on your Matlab
% path that returns the user and password of the user you will submit
% the post as, and the server:
% function [user, password, server] = blogCredentials()
% user = 'your-user-name';
% password = 'secretpassword';
% server = 'http://matlab.cheme.cmu.edu/xmlrpc.php';
% end function
%
% or you will be prompted for that information.
%
% you can specify categories and tags in your m-file with this markup.
% categories: category1, category2
% tags: tag1, tag2
%
% you can refer to other posts with this syntax: `:postid: 456`. This will
% be converted to a link in the published post (but not in the published
% matlab).
%
% you can add a file to be downloaded with `:download: path-to-file`. The
% path should be relative to your m-file. Note that this function *assumes*
% you are publishing the m-file from the directory where the m-file is.
%
% you can have general markup of :directive:`datastring` as long as you
% make a function called wp_directive that handles datastring and returns
% the text it should be replaced with.
%
% this function will add a comment to the end of your m-file containing the
% postid that was published. If you repost the m-file later, and the postid
% is still valid, then the existing post will just be updated.

if nargin == 1
    dryrun = false;
    verbose = false;
elseif nargin == 2
    verbose = false;
end

if ~strcmp(mfile(end-1:end),'.m')
    mfile  = [mfile '.m'];
end

%% make sure file has been published if mfile is newer than html file
% files are published in html
htmlfile = fullfile(pwd, 'html', strrep(mfile,'.m','.html'));
if exist(htmlfile,'file')
    h = dir(htmlfile);
    m = dir(mfile);
    if m.datenum > h.datenum
        % we should publish to get latest output
        htmlfile = publish(mfile);
    end
else
    % file doesn't exist, so we publish it. htmlfile will be an absolute
    % path
    htmlfile = publish(mfile);
end

[publish_location,~,~] = fileparts(htmlfile);

% assume this is a new post. this is changed when reading the mfile if an
% old post_id is found
NEWPOST = true;

%% get title, categories and tags from the mfile
text = fileread(mfile);

%% check title
% we take the first line if it starts with %% and has something in it.
title_re = '^%% ([^\n]*)\r';
tokens = regexp(text, title_re, 'tokens');
if length(tokens) == 1
    title = tokens{1}{1};
else
    title = mfile;
end

%% categories
category_re = '% categories:\s*([^\n]*)\r';
[tokens] = regexp(text,category_re, 'tokens');
if length(tokens) > 1
    % there may be zero
    error('Too many categories matches found')
elseif length(tokens) == 1
    categories = regexp(tokens{1},',','split');
    categories = categories{1};
end

%% tags
tags_re = '% tags:\s*([^\n]*)';
[tokens] = regexp(text,tags_re, 'tokens');
if length(tokens) > 1
    error('too many tags lines found')
elseif length(tokens) == 1
    tags = regexp(tokens{1}{1},',','split');
    tags = tags{1};
elseif isempty(tokens)
    tags = [];
end

%% post_id
tokens = regexp(text,'% post_id =\s*(\d*);.*\r','tokens');
if length(tokens) > 1
    error('Too many post_id lines found')
elseif length(tokens) == 1
    post_id = tokens{1}{1};
    validid = validPostId(post_id);
    if validid == true
        NEWPOST = false;
    else
        %stored postid is not valid, it may have been deleted on the blog.
        NEWPOST = true;
    end
end

%% check to make sure each category exists in the blog
% wordpress silently drops categories that don't exist
% add category if it does not exist.
for i = 1:length(categories)
    category = strtrim(categories{i});
    exists = categoryExists(category);
    if ~exists
        newCategory(category);
    end
end

%% process html
% now we get the html and process it looking for images. we need to upload
% each image, get the new url to the image, and replace that in the html
% code. We also look for special markups e.g. `postid: 650` that are
% replaced by urls to those posts.

htmltext = fileread(htmlfile);

%% handle the Source
% this gets mangled by the substitutions below, so we save it here, replace
% it so nothing is changed, and put it back later
reg = '##### SOURCE BEGIN #####(.*)##### SOURCE END #####';
[match] = regexp(htmltext,reg,'match');
source_code = match{1};

uuid_source_code = char(java.util.UUID.randomUUID);
htmltext = strrep(htmltext, source_code, uuid_source_code);

%% now handle images
reg = ['<img .*?src="'... % up to src="
    '([^"]*)'...     % grab the src filename
    '".*?>'];         % the rest of the link
[tokens] = regexp(htmltext,reg,'tokens');

for i=1:length(tokens)
    %matches{i}
    imgfile = tokens{i}{1};
    % upload new image, and get url
    if ~dryrun
        url = newMediaObject(fullfile(publish_location,imgfile));
    else
        % figure location for a dryrun
        url = fullfile(publish_location,imgfile);
    end
    % replace the old link with the wordpress url
    htmltext = strrep(htmltext,imgfile,url);
end

%% handle any literal text
% we have to find <pre>text</pre>
% we will find and replace these with a UUID, store that UUID in a
% Containers.map so we can substitute the text back in later.
reg = '<pre>([^<]*)</pre>';
[matches] = regexp(htmltext, reg, 'match');
literal_map = containers.Map();
for i=1:length(matches)
    uuid = char(java.util.UUID.randomUUID);
    literal_map(uuid) = matches{i};

    % now replace matches{i} with uuid
    htmltext = strrep(htmltext, matches{i}, uuid);
end

%% Now handle :cmd:`datastrings`
reg = ':([^:]*):`([^`]*)`';
[tokens matches] = regexp(htmltext,reg,'tokens','match');

for i = 1:length(tokens)
    directive = tokens{i}{1};
    datastring = tokens{i}{2};

    % construct string of command to evaluate wp_directive(datastring)
    % all the wp_cmd functions are in ./extensions
    runcmd = sprintf('wp_%s(''%s'')', directive, datastring);
    html = eval(runcmd);

    % now replace the matched text with the html output
    htmltext = strrep(htmltext, matches{i}, html);
    % now
end

%% put literal text back in
keys = literal_map.keys();
for i=1:length(keys)
    htmltext = strrep(htmltext,keys{i}, literal_map(keys{i}));
end

%% finally, put unmodified source code back in.
htmltext = strrep(htmltext, uuid_source_code, [' ' source_code ' ']);

tmpfile = strcat(tempname,'.html');
tid = fopen(tmpfile,'w');
fwrite(tid,htmltext);
fclose(tid);

%% now we get credentials and the client
if exist('blogCredentials','file') == 2
    [user,password,server] = blogCredentials();
else
    user = input('Enter your username: ','s');
    password = passwordUI();
    server = input('Enter blog server: ', 's');
end

import redstone.xmlrpc.XmlRpcClient;
client = XmlRpcClient(server,0);

%% create the post structure
post = java.util.HashMap();
post.put('title',title);

post.put('description',htmltext);
if ~isempty(categories)
    post.put('categories',categories);
end

if ~isempty(tags)
    post.put('mt_keywords',tags);
end

%% finally the posting action
if dryrun
    web(tmpfile)
    edit(tmpfile)
    pause(5)
else
    if NEWPOST == true
        post_id = client.invoke('metaWeblog.newPost',{1,user,password,post,true});

        p = getPost(post_id);
        fid = fopen(mfile,'a+');
        fprintf(fid,'\r%% post_id = %s; %%delete this line to force new post;\r',char(post_id));
        fprintf(fid,'%% permaLink = %s;\r',char(p.get('permaLink')));
        fclose(fid);
    else
        % edit post
        result = client.invoke('metaWeblog.editPost',{post_id,user,password,post,true});
        if result ~= 1
            disp(result)
        end
    end
end

delete(tmpfile);
close all;
 
Private Nachricht senden Benutzer-Profile anzeigen


flashpixx
Forum-Guru

Forum-Guru


Beiträge: 355
Anmeldedatum: 19.04.08
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.08.2012, 00:36     Titel:
  Antworten mit Zitat      
Das was Du machst ist extrem umständlich. Nimm den Code, den Du veröffentlichen willst und füge diesen in das Blogpost ein.
Private Nachricht senden Benutzer-Profile anzeigen
 
quantfinance
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 21
Anmeldedatum: 23.05.10
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.08.2012, 10:53     Titel:
  Antworten mit Zitat      
Danke flashpixx für deine Antwort. Ich will den Code jedoch vorher mit Matlabs "publish"-Befehl kompilieren. Dies formatiert dann den Code mit Textstellen, Absätzen, Code, Tex-kompilierten Formeln etc.

Ich will also nicht nur ein Stück Code veröffentlichen, sondern einen kompletten Blogeintrag wie zB hier zu sehen:

[url]
http://matlab.cheme.cmu.edu/page/2/
[/url]
Private Nachricht senden Benutzer-Profile anzeigen
 
flashpixx
Forum-Guru

Forum-Guru


Beiträge: 355
Anmeldedatum: 19.04.08
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.08.2012, 14:05     Titel:
  Antworten mit Zitat      
quantfinance hat Folgendes geschrieben:
Danke flashpixx für deine Antwort. Ich will den Code jedoch vorher mit Matlabs "publish"-Befehl kompilieren. Dies formatiert dann den Code mit Textstellen, Absätzen, Code, Tex-kompilierten Formeln etc.



Du brauchst das nicht, siehe mein Blog
http://flashpixx.de/2012/08/matlab-mex-beispiel/
http://flashpixx.de/2010/02/stochastischer-gradientenabstieg/

Ich kopiere den Code einfach in den Blogeintrag und sage vorher, welche Sprache es ist. Mein System unterstützt eine Vielzahl an Layoutmechanismen für die unterschiedlichsten Sprachen. In dem Mex Eintrag z.B. C++ und Matlab, LaTeX Codes werden dann direkt als Bild gerendet.

Der Fehler tritt in diesen Zeilen auf (es wäre durchaus hilfreich gewesen, wenn Du diese Zeilen einmal direkt angegeben hättest, als den kompletten Code):
[code]
import redstone.xmlrpc.XmlRpcClient;
client = XmlRpcClient(server,0);
[/quote]

Ohne eine Garantie, dass es funktioniert, sollte dies so funktionieren:
[code]
client = redstone.xmlrpc.XmlRpcClient(server,0);
[/code]

der Packagename wird als Präfix der Klasse mit angegeben, wobei natürlich die Klassen erreichbar sein sollten http://www.mathworks.de/help/techdoc/ref/javaaddpath.html


Ich rate aber dazu, dass Du nicht direkt von Matlab einen Artikel pushst, sondern eben den Artikel mit einer DTP Software oder im der Blogadministration mit einer entsprechenden Formatierung eingibst. Gerade bei Quellcode finde ich passendes Syntaxhighlighting und Zeilennummerierung unerlässlich
Private Nachricht senden Benutzer-Profile anzeigen
 
quantfinance
Themenstarter

Forum-Anfänger

Forum-Anfänger


Beiträge: 21
Anmeldedatum: 23.05.10
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 26.08.2012, 23:48     Titel:
  Antworten mit Zitat      
Erneut Danke an dich, flashpixx. Ich habe die Funktion bei Wordpress nicht gefunden, dass er Matlab-Code erkennt und kompiliert.

Ich fühle auch, dass ich sehr nah an der Lösung bin. Der Code läuft auch durch und ich verstehe mehr und mehr Teile davon. Jedoch bereiten mir die anfangs erwähnten Zeilen immer noch Probleme. Die Fehlermeldung lautet:

Zitat:

Undefined variable "redstone" or class "redstone.xmlrpc.XmlRpcClient".

Error in blogpost (line 232)
client = redstone.xmlrpc.XmlRpcClient(server,0);


Kann mir jemand sagen, wie ich diesen Code zum Laufen bringen kann?
Private Nachricht senden Benutzer-Profile anzeigen
 
flashpixx
Forum-Guru

Forum-Guru


Beiträge: 355
Anmeldedatum: 19.04.08
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 27.08.2012, 01:02     Titel:
  Antworten mit Zitat      
Lies den Artikel über javaaddpath
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2025 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.